From d5e47f6da5b08b13ecdfa7f1edc7e12aeb83fab9 Mon Sep 17 00:00:00 2001 From: Eric Erfanian Date: Wed, 15 Mar 2017 14:41:07 -0700 Subject: Update Dialer source from latest green build. * Refactor voicemail component * Add new enriched calling components Test: treehugger, manual aosp testing Change-Id: I521a0f86327d4b42e14d93927c7d613044ed5942 --- .../contacts/common/ContactPhotoManager.java | 1 + .../common/lettertiles/LetterTileDrawable.java | 70 +- .../common/list/ContactEntryListFragment.java | 74 +- .../widget/SelectPhoneAccountDialogFragment.java | 18 +- java/com/android/dialer/app/AndroidManifest.xml | 12 +- .../com/android/dialer/app/CallDetailActivity.java | 480 -------- java/com/android/dialer/app/DialerApplication.java | 77 -- java/com/android/dialer/app/DialtactsActivity.java | 87 +- java/com/android/dialer/app/PhoneCallDetails.java | 207 ---- .../android/dialer/app/SpecialCharSequenceMgr.java | 46 +- .../app/calllog/CallDetailHistoryAdapter.java | 214 ---- .../dialer/app/calllog/CallLogActivity.java | 220 ++++ .../android/dialer/app/calllog/CallLogAdapter.java | 94 +- .../dialer/app/calllog/CallLogAsyncTaskUtil.java | 243 +--- .../dialer/app/calllog/CallLogFragment.java | 93 +- .../dialer/app/calllog/CallLogListItemHelper.java | 11 +- .../app/calllog/CallLogListItemViewHolder.java | 72 +- .../app/calllog/CallLogNotificationsHelper.java | 299 ----- .../calllog/CallLogNotificationsQueryHelper.java | 334 ++++++ .../app/calllog/CallLogNotificationsService.java | 80 +- .../dialer/app/calllog/CallLogReceiver.java | 4 +- .../android/dialer/app/calllog/CallTypeHelper.java | 136 --- .../dialer/app/calllog/CallTypeIconsView.java | 221 ---- .../app/calllog/DefaultVoicemailNotifier.java | 264 +++-- .../android/dialer/app/calllog/IntentProvider.java | 27 +- .../dialer/app/calllog/MissedCallNotifier.java | 405 ++++--- .../dialer/app/calllog/PhoneAccountHandles.java | 41 + .../dialer/app/calllog/PhoneAccountUtils.java | 104 -- .../dialer/app/calllog/PhoneCallDetailsHelper.java | 5 +- .../dialer/app/calllog/PhoneCallDetailsViews.java | 1 + .../dialer/app/calllog/PhoneNumberDisplayUtil.java | 85 -- .../calllog/VisualVoicemailCallLogFragment.java | 5 +- .../dialer/app/calllog/VoicemailQueryHandler.java | 25 +- .../calllogcache/CallLogCacheLollipopMr1.java | 10 +- .../dialer/app/contactinfo/ContactInfoCache.java | 4 +- .../dialer/app/contactinfo/ContactPhotoLoader.java | 2 +- .../dialer/app/dialpad/DialpadFragment.java | 4 +- .../BlockedNumbersSettingsActivity.java | 5 - .../app/legacybindings/DialerLegacyBindings.java | 3 + .../legacybindings/DialerLegacyBindingsStub.java | 4 + .../com/android/dialer/app/list/ListsFragment.java | 26 +- .../android/dialer/app/list/SearchFragment.java | 11 +- .../app/manifests/activities/AndroidManifest.xml | 12 - .../dialer/app/res/drawable-hdpi/ic_call_arrow.png | Bin 538 -> 0 bytes .../dialer/app/res/drawable-mdpi/ic_call_arrow.png | Bin 455 -> 0 bytes .../app/res/drawable-xhdpi/ic_call_arrow.png | Bin 627 -> 0 bytes .../app/res/drawable-xxhdpi/ic_call_arrow.png | Bin 1203 -> 0 bytes .../app/res/drawable-xxxhdpi/ic_call_arrow.png | Bin 1344 -> 0 bytes .../app/res/drawable-xxxhdpi/search_shadow.9.png | Bin 0 -> 1148 bytes .../android/dialer/app/res/layout/call_detail.xml | 32 - .../dialer/app/res/layout/call_detail_footer.xml | 52 - .../dialer/app/res/layout/call_detail_header.xml | 89 -- .../app/res/layout/call_detail_history_item.xml | 3 +- .../dialer/app/res/layout/call_log_activity.xml | 40 + .../dialer/app/res/layout/call_log_list_item.xml | 3 +- .../dialer/app/res/menu/call_log_options.xml | 22 + .../dialer/app/res/menu/dialtacts_options.xml | 8 +- java/com/android/dialer/app/res/values/colors.xml | 8 - java/com/android/dialer/app/res/values/dimens.xml | 5 +- java/com/android/dialer/app/res/values/strings.xml | 84 +- java/com/android/dialer/app/res/values/styles.xml | 5 - .../app/settings/DialerSettingsActivity.java | 13 + .../app/voicemail/VoicemailPlaybackLayout.java | 7 - .../app/voicemail/VoicemailPlaybackPresenter.java | 84 +- .../error/OmtpVoicemailMessageCreator.java | 113 +- .../app/voicemail/error/VoicemailErrorMessage.java | 51 + .../error/VoicemailErrorMessageCreator.java | 2 +- .../app/voicemail/error/VoicemailStatus.java | 7 + .../error/Vvm3VoicemailMessageCreator.java | 2 +- .../res/layout/voicemai_error_message_fragment.xml | 1 - .../error/res/layout/voicemail_tos_fragment.xml | 1 - .../app/voicemail/error/res/values/strings.xml | 7 + .../dialer/app/widget/ActionBarController.java | 88 +- java/com/android/dialer/backup/AndroidManifest.xml | 3 +- .../android/dialer/backup/DialerBackupAgent.java | 18 +- .../android/dialer/backup/DialerBackupUtils.java | 44 + .../android/dialer/backup/nano/VoicemailInfo.java | 399 +++++++ .../android/dialer/backup/proto/VoicemailInfo.java | 377 ------ .../android/dialer/binary/aosp/AndroidManifest.xml | 116 ++ .../dialer/binary/aosp/AospDialerApplication.java | 30 + .../binary/aosp/AospDialerRootComponent.java | 30 + .../basecomponent/BaseDialerRootComponent.java | 34 + .../dialer/binary/common/DialerApplication.java | 43 + .../dialer/blocking/FilteredNumbersUtil.java | 6 +- java/com/android/dialer/buildtype/BuildType.java | 3 +- .../buildtype/dogfood/BuildTypeAccessorImpl.java | 30 - .../buildtype/release/BuildTypeAccessorImpl.java | 30 + .../dialer/callcomposer/AndroidManifest.xml | 5 +- .../dialer/callcomposer/CallComposerActivity.java | 346 +++--- .../dialer/callcomposer/CallComposerFragment.java | 75 +- .../callcomposer/CallComposerPagerAdapter.java | 4 +- .../callcomposer/CameraComposerFragment.java | 43 +- .../callcomposer/GalleryComposerFragment.java | 44 +- .../dialer/callcomposer/GalleryGridAdapter.java | 12 + .../dialer/callcomposer/GalleryGridItemData.java | 35 +- .../callcomposer/MessageComposerFragment.java | 8 +- .../callcomposer/camera/ImagePersistTask.java | 4 +- .../cameraui/res/layout/camera_view.xml | 8 + .../res/layout/call_composer_activity.xml | 7 +- .../res/layout/fragment_gallery_composer.xml | 2 +- .../res/layout/fragment_message_composer.xml | 77 +- .../callcomposer/res/values-h260dp/values.xml | 19 + .../callcomposer/res/values-h480dp/values.xml | 19 + .../callcomposer/res/values-w360dp/values.xml | 19 + .../callcomposer/res/values-w500dp/values.xml | 19 + .../dialer/callcomposer/res/values/dimens.xml | 4 - .../dialer/callcomposer/res/values/strings.xml | 6 +- .../dialer/callcomposer/res/values/styles.xml | 11 +- .../dialer/callcomposer/res/values/values.xml | 20 + .../android/dialer/calldetails/AndroidManifest.xml | 31 + .../dialer/calldetails/CallDetailsActivity.java | 130 ++ .../dialer/calldetails/CallDetailsAdapter.java | 97 ++ .../calldetails/CallDetailsEntryViewHolder.java | 195 +++ .../calldetails/CallDetailsFooterViewHolder.java | 67 ++ .../calldetails/CallDetailsHeaderViewHolder.java | 125 ++ .../calldetails/nano/CallDetailsEntries.java | 440 +++++++ .../res/drawable/multimedia_image_background.xml | 20 + .../res/layout/call_details_activity.xml | 37 + .../calldetails/res/layout/call_details_entry.xml | 73 ++ .../calldetails/res/layout/call_details_footer.xml | 43 + .../calldetails/res/layout/contact_container.xml | 60 + .../calldetails/res/layout/ec_data_container.xml | 42 + .../calldetails/res/menu/call_details_menu.xml | 23 + .../dialer/calldetails/res/values/dimens.xml | 40 + .../dialer/calldetails/res/values/strings.xml | 42 + .../dialer/calldetails/res/values/styles.xml | 48 + .../dialer/callintent/nano/CallInitiationType.java | 29 +- .../dialer/calllogutils/AndroidManifest.xml | 16 + .../dialer/calllogutils/CallEntryFormatter.java | 113 ++ .../dialer/calllogutils/CallTypeHelper.java | 135 +++ .../dialer/calllogutils/CallTypeIconsView.java | 244 ++++ .../dialer/calllogutils/PhoneAccountUtils.java | 104 ++ .../dialer/calllogutils/PhoneCallDetails.java | 206 ++++ .../calllogutils/PhoneNumberDisplayUtil.java | 83 ++ .../dialer/calllogutils/res/values/colors.xml | 24 + .../dialer/calllogutils/res/values/dimens.xml | 19 + .../dialer/calllogutils/res/values/strings.xml | 90 ++ java/com/android/dialer/common/Assert.java | 30 + ...Value_FallibleAsyncTask_FallibleTaskResult.java | 79 -- .../android/dialer/common/FallibleAsyncTask.java | 6 +- .../dialer/common/PerAccountSharedPreferences.java | 146 +++ java/com/android/dialer/common/proguard.flags | 4 + .../android/dialer/common/res/values/config.xml | 4 + java/com/android/dialer/constants/Constants.java | 14 +- .../dialer/database/CallLogQueryHandler.java | 22 +- .../dialer/debug/bindings/stub/DebugBindings.java | 27 + .../AutoValue_EnrichedCallCapabilities.java | 76 -- .../AutoValue_OutgoingCallComposerData.java | 127 -- .../enrichedcall/EnrichedCallCapabilities.java | 13 +- .../dialer/enrichedcall/EnrichedCallComponent.java | 48 + .../dialer/enrichedcall/EnrichedCallManager.java | 131 +- .../enrichedcall/EnrichedCallManagerStub.java | 84 -- .../enrichedcall/FuzzyPhoneNumberMatcher.java | 20 + .../enrichedcall/OutgoingCallComposerData.java | 16 +- java/com/android/dialer/enrichedcall/Session.java | 7 + .../enrichedcall/StubEnrichedCallModule.java | 32 - .../dialer/enrichedcall/VideoShareSession.java | 20 + .../enrichedcall/historyquery/HistoryQuery.java | 31 + .../historyquery/nano/HistoryResult.java | 203 ++++ .../enrichedcall/stub/EnrichedCallManagerStub.java | 145 +++ .../enrichedcall/stub/StubEnrichedCallModule.java | 35 + .../videoshare/VideoShareListener.java | 14 + .../android/dialer/inject/ApplicationModule.java | 39 - java/com/android/dialer/inject/ContextModule.java | 39 + .../android/dialer/inject/DialerAppComponent.java | 29 - .../android/dialer/inject/HasRootComponent.java | 25 + .../interactions/PhoneNumberInteraction.java | 32 +- .../dialer/logging/nano/ContactLookupResult.java | 29 +- .../android/dialer/logging/nano/ContactSource.java | 29 +- .../dialer/logging/nano/DialerImpression.java | 69 +- .../dialer/logging/nano/InteractionEvent.java | 2 +- .../dialer/logging/nano/ReportingLocation.java | 29 +- .../android/dialer/logging/nano/ScreenEvent.java | 2 +- .../multimedia/AutoValue_MultimediaData.java | 165 --- .../android/dialer/multimedia/MultimediaData.java | 33 +- .../dialer/notification/AndroidManifest.xml | 21 + .../notification/GroupedNotificationUtil.java | 66 ++ .../notification/NotificationChannelManager.java | 232 ++++ .../android/dialer/notification/res/values/ids.xml | 27 + .../dialer/notification/res/values/strings.xml | 25 + java/com/android/dialer/oem/AndroidManifest.xml | 3 + .../dialer/oem/MotorolaHiddenMenuKeySequence.java | 153 +++ java/com/android/dialer/oem/MotorolaUtils.java | 51 + .../dialer/oem/res/values/motorola_config.xml | 64 + .../android/dialer/p13n/inference/P13nRanking.java | 49 +- .../dialer/p13n/inference/protocol/P13nRanker.java | 10 +- .../CachedNumberLookupService.java | 4 + .../android/dialer/postcall/AndroidManifest.xml | 28 + java/com/android/dialer/postcall/PostCall.java | 182 +++ .../android/dialer/postcall/PostCallActivity.java | 151 +++ .../postcall/res/layout/post_call_activity.xml | 38 + .../android/dialer/postcall/res/values/strings.xml | 31 + .../android/dialer/postcall/res/values/values.xml | 19 + java/com/android/dialer/proguard/proguard.flags | 6 + .../android/dialer/proguard/proguard_base.flags | 74 ++ .../android/dialer/proguard/proguard_release.flags | 24 + .../dialer/shortcuts/AutoValue_DialerShortcut.java | 161 --- .../dialer/shortcuts/CallContactActivity.java | 16 +- .../android/dialer/shortcuts/DialerShortcut.java | 6 +- .../dialer/shortcuts/res/values/strings.xml | 6 +- .../android/dialer/shortcuts/res/xml/shortcuts.xml | 4 +- .../dialer/simulator/SimulatorComponent.java | 46 + .../impl/AutoValue_SimulatorCallLog_CallEntry.java | 160 --- .../impl/AutoValue_SimulatorContacts_Contact.java | 231 ---- .../AutoValue_SimulatorVoicemail_Voicemail.java | 184 --- .../dialer/simulator/impl/SimulatorCallLog.java | 6 +- .../dialer/simulator/impl/SimulatorContacts.java | 6 +- .../dialer/simulator/impl/SimulatorImpl.java | 40 + .../dialer/simulator/impl/SimulatorModule.java | 22 +- .../dialer/simulator/impl/SimulatorVoicemail.java | 6 +- java/com/android/dialer/telecom/TelecomUtil.java | 33 +- .../theme/res/drawable-hdpi/ic_block_24dp.png | Bin 0 -> 478 bytes .../theme/res/drawable-hdpi/ic_call_arrow.png | Bin 0 -> 538 bytes .../theme/res/drawable-mdpi/ic_block_24dp.png | Bin 0 -> 335 bytes .../theme/res/drawable-mdpi/ic_call_arrow.png | Bin 0 -> 455 bytes .../theme/res/drawable-xhdpi/ic_block_24dp.png | Bin 0 -> 665 bytes .../theme/res/drawable-xhdpi/ic_call_arrow.png | Bin 0 -> 627 bytes .../theme/res/drawable-xxhdpi/ic_block_24dp.png | Bin 0 -> 973 bytes .../theme/res/drawable-xxhdpi/ic_call_arrow.png | Bin 0 -> 1203 bytes .../theme/res/drawable-xxxhdpi/ic_block_24dp.png | Bin 0 -> 1295 bytes .../theme/res/drawable-xxxhdpi/ic_call_arrow.png | Bin 0 -> 1344 bytes .../com/android/dialer/theme/res/values/dimens.xml | 5 + .../com/android/dialer/theme/res/values/styles.xml | 11 + java/com/android/dialer/util/AndroidManifest.xml | 16 + java/com/android/dialer/util/PermissionsUtil.java | 4 + java/com/android/dialer/util/SettingsUtil.java | 9 + java/com/android/dialer/util/ViewUtil.java | 13 + .../com/android/dialer/widget/MessageFragment.java | 172 +++ .../dialer/widget/res/color/dialer_tint_state.xml | 23 + .../dialer/widget/res/layout/fragment_message.xml | 81 ++ .../widget/res/layout/selectable_text_view.xml | 25 + .../android/dialer/widget/res/values/dimens.xml | 23 + .../android/dialer/widget/res/values/strings.xml | 5 + .../android/incallui/AnswerScreenPresenter.java | 17 +- .../incallui/AnswerScreenPresenterStub.java | 2 +- java/com/android/incallui/CallButtonPresenter.java | 85 +- java/com/android/incallui/CallCardPresenter.java | 138 ++- .../com/android/incallui/CallerInfoAsyncQuery.java | 23 +- java/com/android/incallui/CallerInfoUtils.java | 18 +- java/com/android/incallui/ContactInfoCache.java | 332 ++++-- .../com/android/incallui/ExternalCallNotifier.java | 31 +- java/com/android/incallui/InCallActivity.java | 106 +- .../com/android/incallui/InCallActivityCommon.java | 27 +- java/com/android/incallui/InCallPresenter.java | 89 +- .../incallui/NotificationBroadcastReceiver.java | 10 +- java/com/android/incallui/ProximitySensor.java | 5 +- java/com/android/incallui/StatusBarNotifier.java | 278 +++-- java/com/android/incallui/VideoCallPresenter.java | 247 ++-- .../com/android/incallui/VideoPauseController.java | 236 ++-- .../incallui/answer/bindings/AnswerBindings.java | 4 +- .../incallui/answer/impl/AnswerFragment.java | 78 +- .../answer/impl/AnswerVideoCallScreen.java | 19 +- .../answer/impl/affordance/SwipeButtonHelper.java | 2 +- .../impl/answermethod/AnswerMethodHolder.java | 2 + .../impl/answermethod/FlingUpDownMethod.java | 15 +- .../incallui/answer/impl/hint/AndroidManifest.xml | 2 +- .../answer/impl/hint/AnswerHintFactory.java | 13 +- .../incallui/answer/impl/hint/EventAnswerHint.java | 235 ---- .../answer/impl/hint/EventPayloadLoader.java | 30 - .../answer/impl/hint/EventPayloadLoaderImpl.java | 118 -- .../answer/impl/hint/EventSecretCodeListener.java | 67 -- .../incallui/answer/impl/hint/PawAnswerHint.java | 236 ++++ .../incallui/answer/impl/hint/PawImageLoader.java | 28 + .../answer/impl/hint/PawImageLoaderImpl.java | 48 + .../answer/impl/hint/PawSecretCodeListener.java | 81 ++ .../impl/hint/res/drawable-xxhdpi/cat_paw.webp | Bin 0 -> 68172 bytes .../impl/hint/res/drawable-xxhdpi/dog_paw.webp | Bin 0 -> 22704 bytes .../answer/impl/hint/res/layout/event_hint.xml | 36 - .../answer/impl/hint/res/layout/paw_hint.xml | 37 + .../android/incallui/answer/impl/proguard.flags | 5 + .../incallui/answer/impl/res/values/dimens.xml | 1 + .../incallui/answer/protocol/AnswerScreen.java | 2 +- .../answer/protocol/AnswerScreenDelegate.java | 2 +- .../AnswerProximitySensor.java | 3 +- java/com/android/incallui/call/CallList.java | 50 +- java/com/android/incallui/call/DialerCall.java | 377 +++--- .../android/incallui/call/DialerCallListener.java | 4 +- .../incallui/call/InCallVideoCallCallback.java | 197 --- .../call/InCallVideoCallCallbackNotifier.java | 165 +-- java/com/android/incallui/call/VideoUtils.java | 103 +- .../incallui/calllocation/CallLocation.java | 32 + .../calllocation/CallLocationComponent.java | 46 + .../incallui/calllocation/impl/AndroidManifest.xml | 26 + .../incallui/calllocation/impl/AuthException.java | 25 + .../calllocation/impl/CallLocationImpl.java | 67 ++ .../calllocation/impl/CallLocationModule.java | 29 + .../calllocation/impl/DownloadMapImageTask.java | 77 ++ .../impl/GoogleLocationSettingHelper.java | 123 ++ .../incallui/calllocation/impl/HttpFetcher.java | 289 +++++ .../calllocation/impl/LocationFragment.java | 197 +++ .../incallui/calllocation/impl/LocationHelper.java | 219 ++++ .../calllocation/impl/LocationPresenter.java | 98 ++ .../calllocation/impl/LocationUrlBuilder.java | 177 +++ .../calllocation/impl/ReverseGeocodeTask.java | 144 +++ .../calllocation/impl/TrafficStatsTags.java | 29 + .../impl/res/layout/location_fragment.xml | 134 +++ .../calllocation/impl/res/values/dimens.xml | 6 + .../calllocation/impl/res/values/strings.xml | 15 + .../calllocation/impl/res/values/styles.xml | 28 + .../calllocation/stub/StubCallLocationModule.java | 54 + .../incallui/commontheme/res/anim/blinking.xml | 10 + .../android/incallui/contactgrid/BottomRow.java | 13 +- .../incallui/contactgrid/ContactGridManager.java | 53 +- java/com/android/incallui/contactgrid/TopRow.java | 20 +- .../res/layout/incall_contactgrid_bottom_row.xml | 2 +- .../hold/res/layout/incall_on_hold_banner.xml | 1 - .../AutoValue_MappedButtonConfig_MappingInfo.java | 135 --- .../incallui/incall/impl/FakeDragAnimation.java | 62 + .../incallui/incall/impl/InCallFragment.java | 57 +- .../incallui/incall/impl/InCallPagerAdapter.java | 25 +- .../incallui/incall/impl/MappedButtonConfig.java | 6 +- .../incallui/incall/protocol/PrimaryCallState.java | 26 +- .../incallui/incall/protocol/PrimaryInfo.java | 8 +- java/com/android/incallui/maps/Maps.java | 33 + java/com/android/incallui/maps/MapsComponent.java | 49 + .../android/incallui/maps/StaticMapBinding.java | 51 - .../android/incallui/maps/StaticMapFactory.java | 28 - .../android/incallui/maps/impl/AndroidManifest.xml | 26 + java/com/android/incallui/maps/impl/MapsImpl.java | 40 + .../com/android/incallui/maps/impl/MapsModule.java | 31 + .../incallui/maps/impl/StaticMapFragment.java | 76 ++ .../maps/impl/res/layout/static_map_fragment.xml | 29 + .../android/incallui/maps/stub/StubMapsModule.java | 52 + .../incallui/maps/testing/TestMapsModule.java | 40 + java/com/android/incallui/res/values/strings.xml | 31 - .../incallui/sessiondata/MultimediaFragment.java | 18 +- .../res/layout/fragment_composer_image.xml | 3 +- .../res/layout/fragment_composer_image_frag.xml | 8 + .../res/layout/fragment_composer_text_image.xml | 8 + .../layout/fragment_composer_text_image_frag.xml | 8 + .../incallui/spam/SpamCallListListener.java | 25 +- .../incallui/video/bindings/VideoBindings.java | 4 +- .../incallui/video/impl/VideoCallFragment.java | 43 +- .../video/impl/res/layout/frag_videocall.xml | 11 +- .../incallui/video/impl/res/values/colors.xml | 20 + .../incallui/video/protocol/VideoCallScreen.java | 6 + java/com/android/incallui/videotech/VideoTech.java | 96 ++ .../incallui/videotech/empty/EmptyVideoTech.java | 76 ++ .../videotech/ims/ImsVideoCallCallback.java | 201 ++++ .../incallui/videotech/ims/ImsVideoTech.java | 212 ++++ .../incallui/videotech/rcs/RcsVideoShare.java | 195 +++ java/com/android/voicemail/VoicemailClient.java | 60 + java/com/android/voicemail/VoicemailComponent.java | 46 + .../com/android/voicemail/impl/ActivationTask.java | 298 +++++ .../com/android/voicemail/impl/AndroidManifest.xml | 103 ++ java/com/android/voicemail/impl/Assert.java | 57 + .../voicemail/impl/DefaultOmtpEventHandler.java | 193 +++ .../android/voicemail/impl/NeededForTesting.java | 23 + java/com/android/voicemail/impl/OmtpConstants.java | 239 ++++ java/com/android/voicemail/impl/OmtpEvents.java | 152 +++ java/com/android/voicemail/impl/OmtpService.java | 63 + .../voicemail/impl/OmtpVvmCarrierConfigHelper.java | 444 +++++++ .../voicemail/impl/SubscriptionInfoHelper.java | 70 ++ .../voicemail/impl/TelephonyManagerStub.java | 40 + .../voicemail/impl/TelephonyMangerCompat.java | 57 + .../voicemail/impl/TelephonyVvmConfigManager.java | 150 +++ .../voicemail/impl/VisualVoicemailPreferences.java | 37 + java/com/android/voicemail/impl/Voicemail.java | 341 ++++++ .../voicemail/impl/VoicemailClientImpl.java | 90 ++ .../voicemail/impl/VoicemailClientReceiver.java | 51 + .../android/voicemail/impl/VoicemailModule.java | 41 + .../android/voicemail/impl/VoicemailStatus.java | 160 +++ java/com/android/voicemail/impl/VvmLog.java | 177 +++ .../voicemail/impl/VvmPackageInstallReceiver.java | 65 + .../voicemail/impl/VvmPhoneStateListener.java | 104 ++ .../impl/fetch/FetchVoicemailReceiver.java | 218 ++++ .../impl/fetch/VoicemailFetchedCallback.java | 102 ++ .../android/voicemail/impl/imap/ImapHelper.java | 693 +++++++++++ .../voicemail/impl/imap/VoicemailPayload.java | 36 + java/com/android/voicemail/impl/mail/Address.java | 520 ++++++++ .../impl/mail/AuthenticationFailedException.java | 33 + .../android/voicemail/impl/mail/Base64Body.java | 61 + java/com/android/voicemail/impl/mail/Body.java | 26 + java/com/android/voicemail/impl/mail/BodyPart.java | 24 + .../impl/mail/CertificateValidationException.java | 29 + .../android/voicemail/impl/mail/FetchProfile.java | 79 ++ .../com/android/voicemail/impl/mail/Fetchable.java | 22 + .../impl/mail/FixedLengthInputStream.java | 79 ++ java/com/android/voicemail/impl/mail/Flag.java | 27 + .../android/voicemail/impl/mail/MailTransport.java | 343 ++++++ .../android/voicemail/impl/mail/MeetingInfo.java | 29 + java/com/android/voicemail/impl/mail/Message.java | 146 +++ .../voicemail/impl/mail/MessageDateComparator.java | 35 + .../voicemail/impl/mail/MessagingException.java | 143 +++ .../com/android/voicemail/impl/mail/Multipart.java | 62 + .../android/voicemail/impl/mail/PackedString.java | 172 +++ java/com/android/voicemail/impl/mail/Part.java | 51 + .../voicemail/impl/mail/PeekableInputStream.java | 81 ++ .../android/voicemail/impl/mail/TempDirectory.java | 40 + .../impl/mail/internet/BinaryTempFileBody.java | 87 ++ .../voicemail/impl/mail/internet/MimeBodyPart.java | 200 ++++ .../voicemail/impl/mail/internet/MimeHeader.java | 158 +++ .../voicemail/impl/mail/internet/MimeMessage.java | 676 +++++++++++ .../impl/mail/internet/MimeMultipart.java | 113 ++ .../voicemail/impl/mail/internet/MimeUtility.java | 400 +++++++ .../voicemail/impl/mail/internet/TextBody.java | 59 + .../voicemail/impl/mail/store/ImapConnection.java | 400 +++++++ .../voicemail/impl/mail/store/ImapFolder.java | 797 +++++++++++++ .../voicemail/impl/mail/store/ImapStore.java | 181 +++ .../impl/mail/store/imap/DigestMd5Utils.java | 335 ++++++ .../impl/mail/store/imap/ImapConstants.java | 138 +++ .../impl/mail/store/imap/ImapElement.java | 124 ++ .../voicemail/impl/mail/store/imap/ImapList.java | 226 ++++ .../impl/mail/store/imap/ImapMemoryLiteral.java | 73 ++ .../impl/mail/store/imap/ImapResponse.java | 142 +++ .../impl/mail/store/imap/ImapResponseParser.java | 424 +++++++ .../impl/mail/store/imap/ImapSimpleString.java | 59 + .../voicemail/impl/mail/store/imap/ImapString.java | 179 +++ .../impl/mail/store/imap/ImapTempFileLiteral.java | 119 ++ .../impl/mail/store/imap/ImapUtility.java | 122 ++ .../impl/mail/utility/CountingOutputStream.java | 48 + .../mail/utility/EOLConvertingOutputStream.java | 48 + .../voicemail/impl/mail/utils/LogUtils.java | 345 ++++++ .../android/voicemail/impl/mail/utils/Utility.java | 76 ++ .../voicemail/impl/protocol/CvvmProtocol.java | 59 + .../voicemail/impl/protocol/OmtpProtocol.java | 42 + .../voicemail/impl/protocol/ProtocolHelper.java | 44 + .../impl/protocol/VisualVoicemailProtocol.java | 106 ++ .../protocol/VisualVoicemailProtocolFactory.java | 47 + .../voicemail/impl/protocol/Vvm3EventHandler.java | 307 +++++ .../voicemail/impl/protocol/Vvm3Protocol.java | 305 +++++ .../voicemail/impl/protocol/Vvm3Subscriber.java | 334 ++++++ .../impl/res/layout/voicemail_change_pin.xml | 97 ++ .../android/voicemail/impl/res/values/arrays.xml | 19 + .../android/voicemail/impl/res/values/attrs.xml | 20 + .../android/voicemail/impl/res/values/colors.xml | 19 + .../android/voicemail/impl/res/values/config.xml | 19 + .../android/voicemail/impl/res/values/dimens.xml | 19 + java/com/android/voicemail/impl/res/values/ids.xml | 20 + .../android/voicemail/impl/res/values/strings.xml | 114 ++ .../android/voicemail/impl/res/values/styles.xml | 19 + .../voicemail/impl/res/xml/voicemail_settings.xml | 47 + .../android/voicemail/impl/res/xml/vvm_config.xml | 148 +++ .../voicemail/impl/scheduling/BaseTask.java | 202 ++++ .../voicemail/impl/scheduling/BlockerTask.java | 51 + .../impl/scheduling/MinimalIntervalPolicy.java | 62 + .../android/voicemail/impl/scheduling/Policy.java | 36 + .../voicemail/impl/scheduling/PostponePolicy.java | 68 ++ .../voicemail/impl/scheduling/RetryPolicy.java | 111 ++ .../android/voicemail/impl/scheduling/Task.java | 128 ++ .../impl/scheduling/TaskSchedulerService.java | 396 +++++++ .../impl/settings/VisualVoicemailSettingsUtil.java | 91 ++ .../impl/settings/VoicemailChangePinActivity.java | 624 ++++++++++ .../impl/settings/VoicemailRingtonePreference.java | 110 ++ .../impl/settings/VoicemailSettingsFragment.java | 202 ++++ .../voicemail/impl/sms/LegacyModeSmsHandler.java | 66 ++ .../voicemail/impl/sms/OmtpCvvmMessageSender.java | 55 + .../voicemail/impl/sms/OmtpMessageReceiver.java | 161 +++ .../voicemail/impl/sms/OmtpMessageSender.java | 85 ++ .../impl/sms/OmtpStandardMessageSender.java | 120 ++ .../android/voicemail/impl/sms/StatusMessage.java | 201 ++++ .../voicemail/impl/sms/StatusSmsFetcher.java | 162 +++ .../android/voicemail/impl/sms/SyncMessage.java | 161 +++ .../voicemail/impl/sms/Vvm3MessageSender.java | 57 + .../voicemail/impl/sync/OmtpVvmSyncReceiver.java | 54 + .../voicemail/impl/sync/OmtpVvmSyncService.java | 340 ++++++ .../android/voicemail/impl/sync/SyncOneTask.java | 78 ++ java/com/android/voicemail/impl/sync/SyncTask.java | 75 ++ .../android/voicemail/impl/sync/UploadTask.java | 69 ++ .../impl/sync/VoicemailProviderChangeReceiver.java | 40 + .../impl/sync/VoicemailStatusQueryHelper.java | 113 ++ .../voicemail/impl/sync/VoicemailsQueryHelper.java | 295 +++++ .../voicemail/impl/sync/VvmAccountManager.java | 79 ++ .../voicemail/impl/sync/VvmNetworkRequest.java | 120 ++ .../impl/sync/VvmNetworkRequestCallback.java | 183 +++ .../voicemail/impl/utils/IndentingPrintWriter.java | 155 +++ .../impl/utils/VoicemailDatabaseUtil.java | 85 ++ .../voicemail/impl/utils/VvmDumpHandler.java | 43 + .../com/android/voicemail/impl/utils/XmlUtils.java | 238 ++++ java/com/android/voicemail/permissions.xml | 21 + .../voicemail/stub/StubVoicemailClient.java | 49 + .../voicemail/stub/StubVoicemailModule.java | 33 + .../voicemail/testing/TestVoicemailModule.java | 38 + java/com/android/voicemailomtp/ActivationTask.java | 305 ----- java/com/android/voicemailomtp/AndroidManifest.xml | 105 -- java/com/android/voicemailomtp/Assert.java | 62 - .../voicemailomtp/DefaultOmtpEventHandler.java | 202 ---- .../android/voicemailomtp/NeededForTesting.java | 25 - java/com/android/voicemailomtp/OmtpConstants.java | 248 ---- java/com/android/voicemailomtp/OmtpEvents.java | 156 --- java/com/android/voicemailomtp/OmtpService.java | 65 - .../voicemailomtp/OmtpVvmCarrierConfigHelper.java | 423 ------- .../voicemailomtp/SubscriptionInfoHelper.java | 75 -- .../voicemailomtp/TelephonyManagerStub.java | 80 -- .../voicemailomtp/TelephonyVvmConfigManager.java | 154 --- .../voicemailomtp/VisualVoicemailPreferences.java | 143 --- java/com/android/voicemailomtp/Voicemail.java | 330 ------ .../com/android/voicemailomtp/VoicemailStatus.java | 158 --- java/com/android/voicemailomtp/VvmLog.java | 179 --- .../voicemailomtp/VvmPackageInstallReceiver.java | 70 -- .../voicemailomtp/VvmPhoneStateListener.java | 103 -- .../fetch/FetchVoicemailReceiver.java | 219 ---- .../fetch/VoicemailFetchedCallback.java | 101 -- .../com/android/voicemailomtp/imap/ImapHelper.java | 711 ----------- .../voicemailomtp/imap/VoicemailPayload.java | 38 - java/com/android/voicemailomtp/mail/Address.java | 541 --------- .../mail/AuthenticationFailedException.java | 33 - .../com/android/voicemailomtp/mail/Base64Body.java | 62 - java/com/android/voicemailomtp/mail/Body.java | 25 - java/com/android/voicemailomtp/mail/BodyPart.java | 24 - .../mail/CertificateValidationException.java | 29 - .../android/voicemailomtp/mail/FetchProfile.java | 84 -- java/com/android/voicemailomtp/mail/Fetchable.java | 23 - .../voicemailomtp/mail/FixedLengthInputStream.java | 79 -- java/com/android/voicemailomtp/mail/Flag.java | 29 - .../android/voicemailomtp/mail/MailTransport.java | 344 ------ .../android/voicemailomtp/mail/MeetingInfo.java | 29 - java/com/android/voicemailomtp/mail/Message.java | 144 --- .../voicemailomtp/mail/MessageDateComparator.java | 34 - .../voicemailomtp/mail/MessagingException.java | 139 --- java/com/android/voicemailomtp/mail/Multipart.java | 62 - .../android/voicemailomtp/mail/PackedString.java | 175 --- java/com/android/voicemailomtp/mail/Part.java | 51 - .../voicemailomtp/mail/PeekableInputStream.java | 80 -- .../android/voicemailomtp/mail/TempDirectory.java | 41 - .../mail/internet/BinaryTempFileBody.java | 91 -- .../voicemailomtp/mail/internet/MimeBodyPart.java | 207 ---- .../voicemailomtp/mail/internet/MimeHeader.java | 161 --- .../voicemailomtp/mail/internet/MimeMessage.java | 675 ----------- .../voicemailomtp/mail/internet/MimeMultipart.java | 112 -- .../voicemailomtp/mail/internet/MimeUtility.java | 416 ------- .../voicemailomtp/mail/internet/TextBody.java | 63 - .../voicemailomtp/mail/store/ImapConnection.java | 413 ------- .../voicemailomtp/mail/store/ImapFolder.java | 784 ------------ .../voicemailomtp/mail/store/ImapStore.java | 176 --- .../mail/store/imap/DigestMd5Utils.java | 335 ------ .../mail/store/imap/ImapConstants.java | 144 --- .../voicemailomtp/mail/store/imap/ImapElement.java | 120 -- .../voicemailomtp/mail/store/imap/ImapList.java | 235 ---- .../mail/store/imap/ImapMemoryLiteral.java | 76 -- .../mail/store/imap/ImapResponse.java | 158 --- .../mail/store/imap/ImapResponseParser.java | 432 ------- .../mail/store/imap/ImapSimpleString.java | 62 - .../voicemailomtp/mail/store/imap/ImapString.java | 192 --- .../mail/store/imap/ImapTempFileLiteral.java | 123 -- .../voicemailomtp/mail/store/imap/ImapUtility.java | 125 -- .../mail/utility/CountingOutputStream.java | 48 - .../mail/utility/EOLConvertingOutputStream.java | 48 - .../android/voicemailomtp/mail/utils/LogUtils.java | 413 ------- .../android/voicemailomtp/mail/utils/Utility.java | 80 -- java/com/android/voicemailomtp/permissions.xml | 21 - .../voicemailomtp/protocol/CvvmProtocol.java | 59 - .../voicemailomtp/protocol/OmtpProtocol.java | 37 - .../voicemailomtp/protocol/ProtocolHelper.java | 43 - .../protocol/VisualVoicemailProtocol.java | 100 -- .../protocol/VisualVoicemailProtocolFactory.java | 47 - .../voicemailomtp/protocol/Vvm3EventHandler.java | 271 ----- .../voicemailomtp/protocol/Vvm3Protocol.java | 301 ----- .../voicemailomtp/protocol/Vvm3Subscriber.java | 326 ----- .../res/layout/voicemail_change_pin.xml | 97 -- .../android/voicemailomtp/res/values/arrays.xml | 19 - .../com/android/voicemailomtp/res/values/attrs.xml | 20 - .../android/voicemailomtp/res/values/colors.xml | 19 - .../android/voicemailomtp/res/values/config.xml | 19 - .../android/voicemailomtp/res/values/dimens.xml | 19 - java/com/android/voicemailomtp/res/values/ids.xml | 20 - .../android/voicemailomtp/res/values/strings.xml | 86 -- .../android/voicemailomtp/res/values/styles.xml | 19 - .../voicemailomtp/res/xml/voicemail_settings.xml | 27 - .../android/voicemailomtp/res/xml/vvm_config.xml | 134 --- .../android/voicemailomtp/scheduling/BaseTask.java | 206 ---- .../voicemailomtp/scheduling/BlockerTask.java | 55 - .../scheduling/MinimalIntervalPolicy.java | 69 -- .../android/voicemailomtp/scheduling/Policy.java | 36 - .../voicemailomtp/scheduling/PostponePolicy.java | 69 -- .../voicemailomtp/scheduling/RetryPolicy.java | 117 -- .../com/android/voicemailomtp/scheduling/Task.java | 133 --- .../scheduling/TaskSchedulerService.java | 392 ------ .../settings/VisualVoicemailSettingsUtil.java | 77 -- .../settings/VoicemailChangePinActivity.java | 634 ---------- .../settings/VoicemailSettingsActivity.java | 222 ---- .../voicemailomtp/sms/LegacyModeSmsHandler.java | 67 -- .../voicemailomtp/sms/OmtpCvvmMessageSender.java | 55 - .../voicemailomtp/sms/OmtpMessageReceiver.java | 162 --- .../voicemailomtp/sms/OmtpMessageSender.java | 89 -- .../sms/OmtpStandardMessageSender.java | 119 -- .../android/voicemailomtp/sms/StatusMessage.java | 209 ---- .../voicemailomtp/sms/StatusSmsFetcher.java | 162 --- .../com/android/voicemailomtp/sms/SyncMessage.java | 166 --- .../voicemailomtp/sms/Vvm3MessageSender.java | 56 - .../src/org/apache/commons/io/IOUtils.java | 1202 ------------------- .../org/apache/james/mime4j/BodyDescriptor.java | 392 ------ .../james/mime4j/CloseShieldInputStream.java | 129 -- .../org/apache/james/mime4j/ContentHandler.java | 177 --- .../james/mime4j/EOLConvertingInputStream.java | 139 --- .../src/org/apache/james/mime4j/Log.java | 114 -- .../src/org/apache/james/mime4j/LogFactory.java | 29 - .../james/mime4j/MimeBoundaryInputStream.java | 184 --- .../org/apache/james/mime4j/MimeStreamParser.java | 324 ----- .../org/apache/james/mime4j/RootInputStream.java | 111 -- .../org/apache/james/mime4j/codec/EncoderUtil.java | 630 ---------- .../james/mime4j/decoder/Base64InputStream.java | 151 --- .../org/apache/james/mime4j/decoder/ByteQueue.java | 62 - .../apache/james/mime4j/decoder/DecoderUtil.java | 284 ----- .../mime4j/decoder/QuotedPrintableInputStream.java | 229 ---- .../mime4j/decoder/UnboundedFifoByteBuffer.java | 272 ----- .../james/mime4j/field/AddressListField.java | 65 - .../mime4j/field/ContentTransferEncodingField.java | 88 -- .../james/mime4j/field/ContentTypeField.java | 259 ---- .../apache/james/mime4j/field/DateTimeField.java | 73 -- .../james/mime4j/field/DefaultFieldParser.java | 45 - .../james/mime4j/field/DelegatingFieldParser.java | 47 - .../src/org/apache/james/mime4j/field/Field.java | 192 --- .../org/apache/james/mime4j/field/FieldParser.java | 21 - .../apache/james/mime4j/field/MailboxField.java | 70 -- .../james/mime4j/field/MailboxListField.java | 67 -- .../james/mime4j/field/UnstructuredField.java | 49 - .../apache/james/mime4j/field/address/Address.java | 52 - .../james/mime4j/field/address/AddressList.java | 138 --- .../apache/james/mime4j/field/address/Builder.java | 243 ---- .../james/mime4j/field/address/DomainList.java | 76 -- .../apache/james/mime4j/field/address/Group.java | 75 -- .../apache/james/mime4j/field/address/Mailbox.java | 121 -- .../james/mime4j/field/address/MailboxList.java | 71 -- .../james/mime4j/field/address/NamedMailbox.java | 71 -- .../mime4j/field/address/parser/ASTaddr_spec.java | 19 - .../mime4j/field/address/parser/ASTaddress.java | 19 - .../field/address/parser/ASTaddress_list.java | 19 - .../mime4j/field/address/parser/ASTangle_addr.java | 19 - .../mime4j/field/address/parser/ASTdomain.java | 19 - .../mime4j/field/address/parser/ASTgroup_body.java | 19 - .../mime4j/field/address/parser/ASTlocal_part.java | 19 - .../mime4j/field/address/parser/ASTmailbox.java | 19 - .../mime4j/field/address/parser/ASTname_addr.java | 19 - .../mime4j/field/address/parser/ASTphrase.java | 19 - .../mime4j/field/address/parser/ASTroute.java | 19 - .../field/address/parser/AddressListParser.java | 977 --------------- .../field/address/parser/AddressListParser.jj | 595 ---------- .../address/parser/AddressListParserConstants.java | 76 -- .../parser/AddressListParserTokenManager.java | 1009 ---------------- .../parser/AddressListParserTreeConstants.java | 35 - .../address/parser/AddressListParserVisitor.java | 19 - .../mime4j/field/address/parser/BaseNode.java | 30 - .../address/parser/JJTAddressListParserState.java | 123 -- .../james/mime4j/field/address/parser/Node.java | 37 - .../field/address/parser/ParseException.java | 207 ---- .../field/address/parser/SimpleCharStream.java | 454 ------- .../mime4j/field/address/parser/SimpleNode.java | 87 -- .../james/mime4j/field/address/parser/Token.java | 96 -- .../mime4j/field/address/parser/TokenMgrError.java | 148 --- .../contenttype/parser/ContentTypeParser.java | 268 ----- .../parser/ContentTypeParserConstants.java | 62 - .../parser/ContentTypeParserTokenManager.java | 877 -------------- .../field/contenttype/parser/ParseException.java | 207 ---- .../field/contenttype/parser/SimpleCharStream.java | 454 ------- .../mime4j/field/contenttype/parser/Token.java | 96 -- .../field/contenttype/parser/TokenMgrError.java | 148 --- .../james/mime4j/field/datetime/DateTime.java | 127 -- .../field/datetime/parser/DateTimeParser.java | 570 --------- .../datetime/parser/DateTimeParserConstants.java | 86 -- .../parser/DateTimeParserTokenManager.java | 882 -------------- .../field/datetime/parser/ParseException.java | 207 ---- .../field/datetime/parser/SimpleCharStream.java | 454 ------- .../james/mime4j/field/datetime/parser/Token.java | 96 -- .../field/datetime/parser/TokenMgrError.java | 148 --- .../org/apache/james/mime4j/util/CharsetUtil.java | 1249 -------------------- .../voicemailomtp/sync/OmtpVvmSourceManager.java | 120 -- .../voicemailomtp/sync/OmtpVvmSyncReceiver.java | 61 - .../voicemailomtp/sync/OmtpVvmSyncService.java | 278 ----- .../android/voicemailomtp/sync/SyncOneTask.java | 82 -- java/com/android/voicemailomtp/sync/SyncTask.java | 79 -- .../com/android/voicemailomtp/sync/UploadTask.java | 68 -- .../sync/VoicemailProviderChangeReceiver.java | 41 - .../sync/VoicemailStatusQueryHelper.java | 113 -- .../voicemailomtp/sync/VoicemailsQueryHelper.java | 244 ---- .../voicemailomtp/sync/VvmNetworkRequest.java | 118 -- .../sync/VvmNetworkRequestCallback.java | 171 --- .../voicemailomtp/utils/IndentingPrintWriter.java | 160 --- .../voicemailomtp/utils/VoicemailDatabaseUtil.java | 90 -- .../voicemailomtp/utils/VvmDumpHandler.java | 46 - java/com/android/voicemailomtp/utils/XmlUtils.java | 245 ---- 671 files changed, 32094 insertions(+), 42312 deletions(-) delete mode 100644 java/com/android/dialer/app/CallDetailActivity.java delete mode 100644 java/com/android/dialer/app/DialerApplication.java delete mode 100644 java/com/android/dialer/app/PhoneCallDetails.java delete mode 100644 java/com/android/dialer/app/calllog/CallDetailHistoryAdapter.java create mode 100644 java/com/android/dialer/app/calllog/CallLogActivity.java delete mode 100644 java/com/android/dialer/app/calllog/CallLogNotificationsHelper.java create mode 100644 java/com/android/dialer/app/calllog/CallLogNotificationsQueryHelper.java delete mode 100644 java/com/android/dialer/app/calllog/CallTypeHelper.java delete mode 100644 java/com/android/dialer/app/calllog/CallTypeIconsView.java create mode 100644 java/com/android/dialer/app/calllog/PhoneAccountHandles.java delete mode 100644 java/com/android/dialer/app/calllog/PhoneAccountUtils.java delete mode 100644 java/com/android/dialer/app/calllog/PhoneNumberDisplayUtil.java delete mode 100644 java/com/android/dialer/app/res/drawable-hdpi/ic_call_arrow.png delete mode 100644 java/com/android/dialer/app/res/drawable-mdpi/ic_call_arrow.png delete mode 100644 java/com/android/dialer/app/res/drawable-xhdpi/ic_call_arrow.png delete mode 100644 java/com/android/dialer/app/res/drawable-xxhdpi/ic_call_arrow.png delete mode 100644 java/com/android/dialer/app/res/drawable-xxxhdpi/ic_call_arrow.png create mode 100644 java/com/android/dialer/app/res/drawable-xxxhdpi/search_shadow.9.png delete mode 100644 java/com/android/dialer/app/res/layout/call_detail.xml delete mode 100644 java/com/android/dialer/app/res/layout/call_detail_footer.xml delete mode 100644 java/com/android/dialer/app/res/layout/call_detail_header.xml create mode 100644 java/com/android/dialer/app/res/layout/call_log_activity.xml create mode 100644 java/com/android/dialer/app/res/menu/call_log_options.xml create mode 100644 java/com/android/dialer/backup/nano/VoicemailInfo.java delete mode 100644 java/com/android/dialer/backup/proto/VoicemailInfo.java create mode 100644 java/com/android/dialer/binary/aosp/AndroidManifest.xml create mode 100644 java/com/android/dialer/binary/aosp/AospDialerApplication.java create mode 100644 java/com/android/dialer/binary/aosp/AospDialerRootComponent.java create mode 100644 java/com/android/dialer/binary/basecomponent/BaseDialerRootComponent.java create mode 100644 java/com/android/dialer/binary/common/DialerApplication.java delete mode 100644 java/com/android/dialer/buildtype/dogfood/BuildTypeAccessorImpl.java create mode 100644 java/com/android/dialer/buildtype/release/BuildTypeAccessorImpl.java create mode 100644 java/com/android/dialer/callcomposer/res/values-h260dp/values.xml create mode 100644 java/com/android/dialer/callcomposer/res/values-h480dp/values.xml create mode 100644 java/com/android/dialer/callcomposer/res/values-w360dp/values.xml create mode 100644 java/com/android/dialer/callcomposer/res/values-w500dp/values.xml create mode 100644 java/com/android/dialer/callcomposer/res/values/values.xml create mode 100644 java/com/android/dialer/calldetails/AndroidManifest.xml create mode 100644 java/com/android/dialer/calldetails/CallDetailsActivity.java create mode 100644 java/com/android/dialer/calldetails/CallDetailsAdapter.java create mode 100644 java/com/android/dialer/calldetails/CallDetailsEntryViewHolder.java create mode 100644 java/com/android/dialer/calldetails/CallDetailsFooterViewHolder.java create mode 100644 java/com/android/dialer/calldetails/CallDetailsHeaderViewHolder.java create mode 100644 java/com/android/dialer/calldetails/nano/CallDetailsEntries.java create mode 100644 java/com/android/dialer/calldetails/res/drawable/multimedia_image_background.xml create mode 100644 java/com/android/dialer/calldetails/res/layout/call_details_activity.xml create mode 100644 java/com/android/dialer/calldetails/res/layout/call_details_entry.xml create mode 100644 java/com/android/dialer/calldetails/res/layout/call_details_footer.xml create mode 100644 java/com/android/dialer/calldetails/res/layout/contact_container.xml create mode 100644 java/com/android/dialer/calldetails/res/layout/ec_data_container.xml create mode 100644 java/com/android/dialer/calldetails/res/menu/call_details_menu.xml create mode 100644 java/com/android/dialer/calldetails/res/values/dimens.xml create mode 100644 java/com/android/dialer/calldetails/res/values/strings.xml create mode 100644 java/com/android/dialer/calldetails/res/values/styles.xml create mode 100644 java/com/android/dialer/calllogutils/AndroidManifest.xml create mode 100644 java/com/android/dialer/calllogutils/CallEntryFormatter.java create mode 100644 java/com/android/dialer/calllogutils/CallTypeHelper.java create mode 100644 java/com/android/dialer/calllogutils/CallTypeIconsView.java create mode 100644 java/com/android/dialer/calllogutils/PhoneAccountUtils.java create mode 100644 java/com/android/dialer/calllogutils/PhoneCallDetails.java create mode 100644 java/com/android/dialer/calllogutils/PhoneNumberDisplayUtil.java create mode 100644 java/com/android/dialer/calllogutils/res/values/colors.xml create mode 100644 java/com/android/dialer/calllogutils/res/values/dimens.xml create mode 100644 java/com/android/dialer/calllogutils/res/values/strings.xml delete mode 100644 java/com/android/dialer/common/AutoValue_FallibleAsyncTask_FallibleTaskResult.java create mode 100644 java/com/android/dialer/common/PerAccountSharedPreferences.java create mode 100644 java/com/android/dialer/common/proguard.flags create mode 100644 java/com/android/dialer/common/res/values/config.xml create mode 100644 java/com/android/dialer/debug/bindings/stub/DebugBindings.java delete mode 100644 java/com/android/dialer/enrichedcall/AutoValue_EnrichedCallCapabilities.java delete mode 100644 java/com/android/dialer/enrichedcall/AutoValue_OutgoingCallComposerData.java create mode 100644 java/com/android/dialer/enrichedcall/EnrichedCallComponent.java delete mode 100644 java/com/android/dialer/enrichedcall/EnrichedCallManagerStub.java create mode 100644 java/com/android/dialer/enrichedcall/FuzzyPhoneNumberMatcher.java delete mode 100644 java/com/android/dialer/enrichedcall/StubEnrichedCallModule.java create mode 100644 java/com/android/dialer/enrichedcall/VideoShareSession.java create mode 100644 java/com/android/dialer/enrichedcall/historyquery/HistoryQuery.java create mode 100644 java/com/android/dialer/enrichedcall/historyquery/nano/HistoryResult.java create mode 100644 java/com/android/dialer/enrichedcall/stub/EnrichedCallManagerStub.java create mode 100644 java/com/android/dialer/enrichedcall/stub/StubEnrichedCallModule.java create mode 100644 java/com/android/dialer/enrichedcall/videoshare/VideoShareListener.java delete mode 100644 java/com/android/dialer/inject/ApplicationModule.java create mode 100644 java/com/android/dialer/inject/ContextModule.java delete mode 100644 java/com/android/dialer/inject/DialerAppComponent.java create mode 100644 java/com/android/dialer/inject/HasRootComponent.java delete mode 100644 java/com/android/dialer/multimedia/AutoValue_MultimediaData.java create mode 100644 java/com/android/dialer/notification/AndroidManifest.xml create mode 100644 java/com/android/dialer/notification/GroupedNotificationUtil.java create mode 100644 java/com/android/dialer/notification/NotificationChannelManager.java create mode 100644 java/com/android/dialer/notification/res/values/ids.xml create mode 100644 java/com/android/dialer/notification/res/values/strings.xml create mode 100644 java/com/android/dialer/oem/AndroidManifest.xml create mode 100644 java/com/android/dialer/oem/MotorolaHiddenMenuKeySequence.java create mode 100644 java/com/android/dialer/oem/MotorolaUtils.java create mode 100644 java/com/android/dialer/oem/res/values/motorola_config.xml create mode 100644 java/com/android/dialer/postcall/AndroidManifest.xml create mode 100644 java/com/android/dialer/postcall/PostCall.java create mode 100644 java/com/android/dialer/postcall/PostCallActivity.java create mode 100644 java/com/android/dialer/postcall/res/layout/post_call_activity.xml create mode 100644 java/com/android/dialer/postcall/res/values/strings.xml create mode 100644 java/com/android/dialer/postcall/res/values/values.xml create mode 100644 java/com/android/dialer/proguard/proguard.flags create mode 100644 java/com/android/dialer/proguard/proguard_base.flags create mode 100644 java/com/android/dialer/proguard/proguard_release.flags delete mode 100644 java/com/android/dialer/shortcuts/AutoValue_DialerShortcut.java create mode 100644 java/com/android/dialer/simulator/SimulatorComponent.java delete mode 100644 java/com/android/dialer/simulator/impl/AutoValue_SimulatorCallLog_CallEntry.java delete mode 100644 java/com/android/dialer/simulator/impl/AutoValue_SimulatorContacts_Contact.java delete mode 100644 java/com/android/dialer/simulator/impl/AutoValue_SimulatorVoicemail_Voicemail.java create mode 100644 java/com/android/dialer/simulator/impl/SimulatorImpl.java create mode 100644 java/com/android/dialer/theme/res/drawable-hdpi/ic_block_24dp.png create mode 100644 java/com/android/dialer/theme/res/drawable-hdpi/ic_call_arrow.png create mode 100644 java/com/android/dialer/theme/res/drawable-mdpi/ic_block_24dp.png create mode 100644 java/com/android/dialer/theme/res/drawable-mdpi/ic_call_arrow.png create mode 100644 java/com/android/dialer/theme/res/drawable-xhdpi/ic_block_24dp.png create mode 100644 java/com/android/dialer/theme/res/drawable-xhdpi/ic_call_arrow.png create mode 100644 java/com/android/dialer/theme/res/drawable-xxhdpi/ic_block_24dp.png create mode 100644 java/com/android/dialer/theme/res/drawable-xxhdpi/ic_call_arrow.png create mode 100644 java/com/android/dialer/theme/res/drawable-xxxhdpi/ic_block_24dp.png create mode 100644 java/com/android/dialer/theme/res/drawable-xxxhdpi/ic_call_arrow.png create mode 100644 java/com/android/dialer/widget/MessageFragment.java create mode 100644 java/com/android/dialer/widget/res/color/dialer_tint_state.xml create mode 100644 java/com/android/dialer/widget/res/layout/fragment_message.xml create mode 100644 java/com/android/dialer/widget/res/layout/selectable_text_view.xml create mode 100644 java/com/android/dialer/widget/res/values/dimens.xml create mode 100644 java/com/android/dialer/widget/res/values/strings.xml delete mode 100644 java/com/android/incallui/answer/impl/hint/EventAnswerHint.java delete mode 100644 java/com/android/incallui/answer/impl/hint/EventPayloadLoader.java delete mode 100644 java/com/android/incallui/answer/impl/hint/EventPayloadLoaderImpl.java delete mode 100644 java/com/android/incallui/answer/impl/hint/EventSecretCodeListener.java create mode 100644 java/com/android/incallui/answer/impl/hint/PawAnswerHint.java create mode 100644 java/com/android/incallui/answer/impl/hint/PawImageLoader.java create mode 100644 java/com/android/incallui/answer/impl/hint/PawImageLoaderImpl.java create mode 100644 java/com/android/incallui/answer/impl/hint/PawSecretCodeListener.java create mode 100644 java/com/android/incallui/answer/impl/hint/res/drawable-xxhdpi/cat_paw.webp create mode 100644 java/com/android/incallui/answer/impl/hint/res/drawable-xxhdpi/dog_paw.webp delete mode 100644 java/com/android/incallui/answer/impl/hint/res/layout/event_hint.xml create mode 100644 java/com/android/incallui/answer/impl/hint/res/layout/paw_hint.xml create mode 100644 java/com/android/incallui/answer/impl/proguard.flags delete mode 100644 java/com/android/incallui/call/InCallVideoCallCallback.java create mode 100644 java/com/android/incallui/calllocation/CallLocation.java create mode 100644 java/com/android/incallui/calllocation/CallLocationComponent.java create mode 100644 java/com/android/incallui/calllocation/impl/AndroidManifest.xml create mode 100644 java/com/android/incallui/calllocation/impl/AuthException.java create mode 100644 java/com/android/incallui/calllocation/impl/CallLocationImpl.java create mode 100644 java/com/android/incallui/calllocation/impl/CallLocationModule.java create mode 100644 java/com/android/incallui/calllocation/impl/DownloadMapImageTask.java create mode 100644 java/com/android/incallui/calllocation/impl/GoogleLocationSettingHelper.java create mode 100644 java/com/android/incallui/calllocation/impl/HttpFetcher.java create mode 100644 java/com/android/incallui/calllocation/impl/LocationFragment.java create mode 100644 java/com/android/incallui/calllocation/impl/LocationHelper.java create mode 100644 java/com/android/incallui/calllocation/impl/LocationPresenter.java create mode 100644 java/com/android/incallui/calllocation/impl/LocationUrlBuilder.java create mode 100644 java/com/android/incallui/calllocation/impl/ReverseGeocodeTask.java create mode 100644 java/com/android/incallui/calllocation/impl/TrafficStatsTags.java create mode 100644 java/com/android/incallui/calllocation/impl/res/layout/location_fragment.xml create mode 100644 java/com/android/incallui/calllocation/impl/res/values/dimens.xml create mode 100644 java/com/android/incallui/calllocation/impl/res/values/strings.xml create mode 100644 java/com/android/incallui/calllocation/impl/res/values/styles.xml create mode 100644 java/com/android/incallui/calllocation/stub/StubCallLocationModule.java create mode 100644 java/com/android/incallui/commontheme/res/anim/blinking.xml delete mode 100644 java/com/android/incallui/incall/impl/AutoValue_MappedButtonConfig_MappingInfo.java create mode 100644 java/com/android/incallui/incall/impl/FakeDragAnimation.java create mode 100644 java/com/android/incallui/maps/Maps.java create mode 100644 java/com/android/incallui/maps/MapsComponent.java delete mode 100644 java/com/android/incallui/maps/StaticMapBinding.java delete mode 100644 java/com/android/incallui/maps/StaticMapFactory.java create mode 100644 java/com/android/incallui/maps/impl/AndroidManifest.xml create mode 100644 java/com/android/incallui/maps/impl/MapsImpl.java create mode 100644 java/com/android/incallui/maps/impl/MapsModule.java create mode 100644 java/com/android/incallui/maps/impl/StaticMapFragment.java create mode 100644 java/com/android/incallui/maps/impl/res/layout/static_map_fragment.xml create mode 100644 java/com/android/incallui/maps/stub/StubMapsModule.java create mode 100644 java/com/android/incallui/maps/testing/TestMapsModule.java create mode 100644 java/com/android/incallui/video/impl/res/values/colors.xml create mode 100644 java/com/android/incallui/videotech/VideoTech.java create mode 100644 java/com/android/incallui/videotech/empty/EmptyVideoTech.java create mode 100644 java/com/android/incallui/videotech/ims/ImsVideoCallCallback.java create mode 100644 java/com/android/incallui/videotech/ims/ImsVideoTech.java create mode 100644 java/com/android/incallui/videotech/rcs/RcsVideoShare.java create mode 100644 java/com/android/voicemail/VoicemailClient.java create mode 100644 java/com/android/voicemail/VoicemailComponent.java create mode 100644 java/com/android/voicemail/impl/ActivationTask.java create mode 100644 java/com/android/voicemail/impl/AndroidManifest.xml create mode 100644 java/com/android/voicemail/impl/Assert.java create mode 100644 java/com/android/voicemail/impl/DefaultOmtpEventHandler.java create mode 100644 java/com/android/voicemail/impl/NeededForTesting.java create mode 100644 java/com/android/voicemail/impl/OmtpConstants.java create mode 100644 java/com/android/voicemail/impl/OmtpEvents.java create mode 100644 java/com/android/voicemail/impl/OmtpService.java create mode 100644 java/com/android/voicemail/impl/OmtpVvmCarrierConfigHelper.java create mode 100644 java/com/android/voicemail/impl/SubscriptionInfoHelper.java create mode 100644 java/com/android/voicemail/impl/TelephonyManagerStub.java create mode 100644 java/com/android/voicemail/impl/TelephonyMangerCompat.java create mode 100644 java/com/android/voicemail/impl/TelephonyVvmConfigManager.java create mode 100644 java/com/android/voicemail/impl/VisualVoicemailPreferences.java create mode 100644 java/com/android/voicemail/impl/Voicemail.java create mode 100644 java/com/android/voicemail/impl/VoicemailClientImpl.java create mode 100644 java/com/android/voicemail/impl/VoicemailClientReceiver.java create mode 100644 java/com/android/voicemail/impl/VoicemailModule.java create mode 100644 java/com/android/voicemail/impl/VoicemailStatus.java create mode 100644 java/com/android/voicemail/impl/VvmLog.java create mode 100644 java/com/android/voicemail/impl/VvmPackageInstallReceiver.java create mode 100644 java/com/android/voicemail/impl/VvmPhoneStateListener.java create mode 100644 java/com/android/voicemail/impl/fetch/FetchVoicemailReceiver.java create mode 100644 java/com/android/voicemail/impl/fetch/VoicemailFetchedCallback.java create mode 100644 java/com/android/voicemail/impl/imap/ImapHelper.java create mode 100644 java/com/android/voicemail/impl/imap/VoicemailPayload.java create mode 100644 java/com/android/voicemail/impl/mail/Address.java create mode 100644 java/com/android/voicemail/impl/mail/AuthenticationFailedException.java create mode 100644 java/com/android/voicemail/impl/mail/Base64Body.java create mode 100644 java/com/android/voicemail/impl/mail/Body.java create mode 100644 java/com/android/voicemail/impl/mail/BodyPart.java create mode 100644 java/com/android/voicemail/impl/mail/CertificateValidationException.java create mode 100644 java/com/android/voicemail/impl/mail/FetchProfile.java create mode 100644 java/com/android/voicemail/impl/mail/Fetchable.java create mode 100644 java/com/android/voicemail/impl/mail/FixedLengthInputStream.java create mode 100644 java/com/android/voicemail/impl/mail/Flag.java create mode 100644 java/com/android/voicemail/impl/mail/MailTransport.java create mode 100644 java/com/android/voicemail/impl/mail/MeetingInfo.java create mode 100644 java/com/android/voicemail/impl/mail/Message.java create mode 100644 java/com/android/voicemail/impl/mail/MessageDateComparator.java create mode 100644 java/com/android/voicemail/impl/mail/MessagingException.java create mode 100644 java/com/android/voicemail/impl/mail/Multipart.java create mode 100644 java/com/android/voicemail/impl/mail/PackedString.java create mode 100644 java/com/android/voicemail/impl/mail/Part.java create mode 100644 java/com/android/voicemail/impl/mail/PeekableInputStream.java create mode 100644 java/com/android/voicemail/impl/mail/TempDirectory.java create mode 100644 java/com/android/voicemail/impl/mail/internet/BinaryTempFileBody.java create mode 100644 java/com/android/voicemail/impl/mail/internet/MimeBodyPart.java create mode 100644 java/com/android/voicemail/impl/mail/internet/MimeHeader.java create mode 100644 java/com/android/voicemail/impl/mail/internet/MimeMessage.java create mode 100644 java/com/android/voicemail/impl/mail/internet/MimeMultipart.java create mode 100644 java/com/android/voicemail/impl/mail/internet/MimeUtility.java create mode 100644 java/com/android/voicemail/impl/mail/internet/TextBody.java create mode 100644 java/com/android/voicemail/impl/mail/store/ImapConnection.java create mode 100644 java/com/android/voicemail/impl/mail/store/ImapFolder.java create mode 100644 java/com/android/voicemail/impl/mail/store/ImapStore.java create mode 100644 java/com/android/voicemail/impl/mail/store/imap/DigestMd5Utils.java create mode 100644 java/com/android/voicemail/impl/mail/store/imap/ImapConstants.java create mode 100644 java/com/android/voicemail/impl/mail/store/imap/ImapElement.java create mode 100644 java/com/android/voicemail/impl/mail/store/imap/ImapList.java create mode 100644 java/com/android/voicemail/impl/mail/store/imap/ImapMemoryLiteral.java create mode 100644 java/com/android/voicemail/impl/mail/store/imap/ImapResponse.java create mode 100644 java/com/android/voicemail/impl/mail/store/imap/ImapResponseParser.java create mode 100644 java/com/android/voicemail/impl/mail/store/imap/ImapSimpleString.java create mode 100644 java/com/android/voicemail/impl/mail/store/imap/ImapString.java create mode 100644 java/com/android/voicemail/impl/mail/store/imap/ImapTempFileLiteral.java create mode 100644 java/com/android/voicemail/impl/mail/store/imap/ImapUtility.java create mode 100644 java/com/android/voicemail/impl/mail/utility/CountingOutputStream.java create mode 100644 java/com/android/voicemail/impl/mail/utility/EOLConvertingOutputStream.java create mode 100644 java/com/android/voicemail/impl/mail/utils/LogUtils.java create mode 100644 java/com/android/voicemail/impl/mail/utils/Utility.java create mode 100644 java/com/android/voicemail/impl/protocol/CvvmProtocol.java create mode 100644 java/com/android/voicemail/impl/protocol/OmtpProtocol.java create mode 100644 java/com/android/voicemail/impl/protocol/ProtocolHelper.java create mode 100644 java/com/android/voicemail/impl/protocol/VisualVoicemailProtocol.java create mode 100644 java/com/android/voicemail/impl/protocol/VisualVoicemailProtocolFactory.java create mode 100644 java/com/android/voicemail/impl/protocol/Vvm3EventHandler.java create mode 100644 java/com/android/voicemail/impl/protocol/Vvm3Protocol.java create mode 100644 java/com/android/voicemail/impl/protocol/Vvm3Subscriber.java create mode 100644 java/com/android/voicemail/impl/res/layout/voicemail_change_pin.xml create mode 100644 java/com/android/voicemail/impl/res/values/arrays.xml create mode 100644 java/com/android/voicemail/impl/res/values/attrs.xml create mode 100644 java/com/android/voicemail/impl/res/values/colors.xml create mode 100644 java/com/android/voicemail/impl/res/values/config.xml create mode 100644 java/com/android/voicemail/impl/res/values/dimens.xml create mode 100644 java/com/android/voicemail/impl/res/values/ids.xml create mode 100644 java/com/android/voicemail/impl/res/values/strings.xml create mode 100644 java/com/android/voicemail/impl/res/values/styles.xml create mode 100644 java/com/android/voicemail/impl/res/xml/voicemail_settings.xml create mode 100644 java/com/android/voicemail/impl/res/xml/vvm_config.xml create mode 100644 java/com/android/voicemail/impl/scheduling/BaseTask.java create mode 100644 java/com/android/voicemail/impl/scheduling/BlockerTask.java create mode 100644 java/com/android/voicemail/impl/scheduling/MinimalIntervalPolicy.java create mode 100644 java/com/android/voicemail/impl/scheduling/Policy.java create mode 100644 java/com/android/voicemail/impl/scheduling/PostponePolicy.java create mode 100644 java/com/android/voicemail/impl/scheduling/RetryPolicy.java create mode 100644 java/com/android/voicemail/impl/scheduling/Task.java create mode 100644 java/com/android/voicemail/impl/scheduling/TaskSchedulerService.java create mode 100644 java/com/android/voicemail/impl/settings/VisualVoicemailSettingsUtil.java create mode 100644 java/com/android/voicemail/impl/settings/VoicemailChangePinActivity.java create mode 100644 java/com/android/voicemail/impl/settings/VoicemailRingtonePreference.java create mode 100644 java/com/android/voicemail/impl/settings/VoicemailSettingsFragment.java create mode 100644 java/com/android/voicemail/impl/sms/LegacyModeSmsHandler.java create mode 100644 java/com/android/voicemail/impl/sms/OmtpCvvmMessageSender.java create mode 100644 java/com/android/voicemail/impl/sms/OmtpMessageReceiver.java create mode 100644 java/com/android/voicemail/impl/sms/OmtpMessageSender.java create mode 100644 java/com/android/voicemail/impl/sms/OmtpStandardMessageSender.java create mode 100644 java/com/android/voicemail/impl/sms/StatusMessage.java create mode 100644 java/com/android/voicemail/impl/sms/StatusSmsFetcher.java create mode 100644 java/com/android/voicemail/impl/sms/SyncMessage.java create mode 100644 java/com/android/voicemail/impl/sms/Vvm3MessageSender.java create mode 100644 java/com/android/voicemail/impl/sync/OmtpVvmSyncReceiver.java create mode 100644 java/com/android/voicemail/impl/sync/OmtpVvmSyncService.java create mode 100644 java/com/android/voicemail/impl/sync/SyncOneTask.java create mode 100644 java/com/android/voicemail/impl/sync/SyncTask.java create mode 100644 java/com/android/voicemail/impl/sync/UploadTask.java create mode 100644 java/com/android/voicemail/impl/sync/VoicemailProviderChangeReceiver.java create mode 100644 java/com/android/voicemail/impl/sync/VoicemailStatusQueryHelper.java create mode 100644 java/com/android/voicemail/impl/sync/VoicemailsQueryHelper.java create mode 100644 java/com/android/voicemail/impl/sync/VvmAccountManager.java create mode 100644 java/com/android/voicemail/impl/sync/VvmNetworkRequest.java create mode 100644 java/com/android/voicemail/impl/sync/VvmNetworkRequestCallback.java create mode 100644 java/com/android/voicemail/impl/utils/IndentingPrintWriter.java create mode 100644 java/com/android/voicemail/impl/utils/VoicemailDatabaseUtil.java create mode 100644 java/com/android/voicemail/impl/utils/VvmDumpHandler.java create mode 100644 java/com/android/voicemail/impl/utils/XmlUtils.java create mode 100644 java/com/android/voicemail/permissions.xml create mode 100644 java/com/android/voicemail/stub/StubVoicemailClient.java create mode 100644 java/com/android/voicemail/stub/StubVoicemailModule.java create mode 100644 java/com/android/voicemail/testing/TestVoicemailModule.java delete mode 100644 java/com/android/voicemailomtp/ActivationTask.java delete mode 100644 java/com/android/voicemailomtp/AndroidManifest.xml delete mode 100644 java/com/android/voicemailomtp/Assert.java delete mode 100644 java/com/android/voicemailomtp/DefaultOmtpEventHandler.java delete mode 100644 java/com/android/voicemailomtp/NeededForTesting.java delete mode 100644 java/com/android/voicemailomtp/OmtpConstants.java delete mode 100644 java/com/android/voicemailomtp/OmtpEvents.java delete mode 100644 java/com/android/voicemailomtp/OmtpService.java delete mode 100644 java/com/android/voicemailomtp/OmtpVvmCarrierConfigHelper.java delete mode 100644 java/com/android/voicemailomtp/SubscriptionInfoHelper.java delete mode 100644 java/com/android/voicemailomtp/TelephonyManagerStub.java delete mode 100644 java/com/android/voicemailomtp/TelephonyVvmConfigManager.java delete mode 100644 java/com/android/voicemailomtp/VisualVoicemailPreferences.java delete mode 100644 java/com/android/voicemailomtp/Voicemail.java delete mode 100644 java/com/android/voicemailomtp/VoicemailStatus.java delete mode 100644 java/com/android/voicemailomtp/VvmLog.java delete mode 100644 java/com/android/voicemailomtp/VvmPackageInstallReceiver.java delete mode 100644 java/com/android/voicemailomtp/VvmPhoneStateListener.java delete mode 100644 java/com/android/voicemailomtp/fetch/FetchVoicemailReceiver.java delete mode 100644 java/com/android/voicemailomtp/fetch/VoicemailFetchedCallback.java delete mode 100644 java/com/android/voicemailomtp/imap/ImapHelper.java delete mode 100644 java/com/android/voicemailomtp/imap/VoicemailPayload.java delete mode 100644 java/com/android/voicemailomtp/mail/Address.java delete mode 100644 java/com/android/voicemailomtp/mail/AuthenticationFailedException.java delete mode 100644 java/com/android/voicemailomtp/mail/Base64Body.java delete mode 100644 java/com/android/voicemailomtp/mail/Body.java delete mode 100644 java/com/android/voicemailomtp/mail/BodyPart.java delete mode 100644 java/com/android/voicemailomtp/mail/CertificateValidationException.java delete mode 100644 java/com/android/voicemailomtp/mail/FetchProfile.java delete mode 100644 java/com/android/voicemailomtp/mail/Fetchable.java delete mode 100644 java/com/android/voicemailomtp/mail/FixedLengthInputStream.java delete mode 100644 java/com/android/voicemailomtp/mail/Flag.java delete mode 100644 java/com/android/voicemailomtp/mail/MailTransport.java delete mode 100644 java/com/android/voicemailomtp/mail/MeetingInfo.java delete mode 100644 java/com/android/voicemailomtp/mail/Message.java delete mode 100644 java/com/android/voicemailomtp/mail/MessageDateComparator.java delete mode 100644 java/com/android/voicemailomtp/mail/MessagingException.java delete mode 100644 java/com/android/voicemailomtp/mail/Multipart.java delete mode 100644 java/com/android/voicemailomtp/mail/PackedString.java delete mode 100644 java/com/android/voicemailomtp/mail/Part.java delete mode 100644 java/com/android/voicemailomtp/mail/PeekableInputStream.java delete mode 100644 java/com/android/voicemailomtp/mail/TempDirectory.java delete mode 100644 java/com/android/voicemailomtp/mail/internet/BinaryTempFileBody.java delete mode 100644 java/com/android/voicemailomtp/mail/internet/MimeBodyPart.java delete mode 100644 java/com/android/voicemailomtp/mail/internet/MimeHeader.java delete mode 100644 java/com/android/voicemailomtp/mail/internet/MimeMessage.java delete mode 100644 java/com/android/voicemailomtp/mail/internet/MimeMultipart.java delete mode 100644 java/com/android/voicemailomtp/mail/internet/MimeUtility.java delete mode 100644 java/com/android/voicemailomtp/mail/internet/TextBody.java delete mode 100644 java/com/android/voicemailomtp/mail/store/ImapConnection.java delete mode 100644 java/com/android/voicemailomtp/mail/store/ImapFolder.java delete mode 100644 java/com/android/voicemailomtp/mail/store/ImapStore.java delete mode 100644 java/com/android/voicemailomtp/mail/store/imap/DigestMd5Utils.java delete mode 100644 java/com/android/voicemailomtp/mail/store/imap/ImapConstants.java delete mode 100644 java/com/android/voicemailomtp/mail/store/imap/ImapElement.java delete mode 100644 java/com/android/voicemailomtp/mail/store/imap/ImapList.java delete mode 100644 java/com/android/voicemailomtp/mail/store/imap/ImapMemoryLiteral.java delete mode 100644 java/com/android/voicemailomtp/mail/store/imap/ImapResponse.java delete mode 100644 java/com/android/voicemailomtp/mail/store/imap/ImapResponseParser.java delete mode 100644 java/com/android/voicemailomtp/mail/store/imap/ImapSimpleString.java delete mode 100644 java/com/android/voicemailomtp/mail/store/imap/ImapString.java delete mode 100644 java/com/android/voicemailomtp/mail/store/imap/ImapTempFileLiteral.java delete mode 100644 java/com/android/voicemailomtp/mail/store/imap/ImapUtility.java delete mode 100644 java/com/android/voicemailomtp/mail/utility/CountingOutputStream.java delete mode 100644 java/com/android/voicemailomtp/mail/utility/EOLConvertingOutputStream.java delete mode 100644 java/com/android/voicemailomtp/mail/utils/LogUtils.java delete mode 100644 java/com/android/voicemailomtp/mail/utils/Utility.java delete mode 100644 java/com/android/voicemailomtp/permissions.xml delete mode 100644 java/com/android/voicemailomtp/protocol/CvvmProtocol.java delete mode 100644 java/com/android/voicemailomtp/protocol/OmtpProtocol.java delete mode 100644 java/com/android/voicemailomtp/protocol/ProtocolHelper.java delete mode 100644 java/com/android/voicemailomtp/protocol/VisualVoicemailProtocol.java delete mode 100644 java/com/android/voicemailomtp/protocol/VisualVoicemailProtocolFactory.java delete mode 100644 java/com/android/voicemailomtp/protocol/Vvm3EventHandler.java delete mode 100644 java/com/android/voicemailomtp/protocol/Vvm3Protocol.java delete mode 100644 java/com/android/voicemailomtp/protocol/Vvm3Subscriber.java delete mode 100644 java/com/android/voicemailomtp/res/layout/voicemail_change_pin.xml delete mode 100644 java/com/android/voicemailomtp/res/values/arrays.xml delete mode 100644 java/com/android/voicemailomtp/res/values/attrs.xml delete mode 100644 java/com/android/voicemailomtp/res/values/colors.xml delete mode 100644 java/com/android/voicemailomtp/res/values/config.xml delete mode 100644 java/com/android/voicemailomtp/res/values/dimens.xml delete mode 100644 java/com/android/voicemailomtp/res/values/ids.xml delete mode 100644 java/com/android/voicemailomtp/res/values/strings.xml delete mode 100644 java/com/android/voicemailomtp/res/values/styles.xml delete mode 100644 java/com/android/voicemailomtp/res/xml/voicemail_settings.xml delete mode 100644 java/com/android/voicemailomtp/res/xml/vvm_config.xml delete mode 100644 java/com/android/voicemailomtp/scheduling/BaseTask.java delete mode 100644 java/com/android/voicemailomtp/scheduling/BlockerTask.java delete mode 100644 java/com/android/voicemailomtp/scheduling/MinimalIntervalPolicy.java delete mode 100644 java/com/android/voicemailomtp/scheduling/Policy.java delete mode 100644 java/com/android/voicemailomtp/scheduling/PostponePolicy.java delete mode 100644 java/com/android/voicemailomtp/scheduling/RetryPolicy.java delete mode 100644 java/com/android/voicemailomtp/scheduling/Task.java delete mode 100644 java/com/android/voicemailomtp/scheduling/TaskSchedulerService.java delete mode 100644 java/com/android/voicemailomtp/settings/VisualVoicemailSettingsUtil.java delete mode 100644 java/com/android/voicemailomtp/settings/VoicemailChangePinActivity.java delete mode 100644 java/com/android/voicemailomtp/settings/VoicemailSettingsActivity.java delete mode 100644 java/com/android/voicemailomtp/sms/LegacyModeSmsHandler.java delete mode 100644 java/com/android/voicemailomtp/sms/OmtpCvvmMessageSender.java delete mode 100644 java/com/android/voicemailomtp/sms/OmtpMessageReceiver.java delete mode 100644 java/com/android/voicemailomtp/sms/OmtpMessageSender.java delete mode 100644 java/com/android/voicemailomtp/sms/OmtpStandardMessageSender.java delete mode 100644 java/com/android/voicemailomtp/sms/StatusMessage.java delete mode 100644 java/com/android/voicemailomtp/sms/StatusSmsFetcher.java delete mode 100644 java/com/android/voicemailomtp/sms/SyncMessage.java delete mode 100644 java/com/android/voicemailomtp/sms/Vvm3MessageSender.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/commons/io/IOUtils.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/BodyDescriptor.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/CloseShieldInputStream.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/ContentHandler.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/EOLConvertingInputStream.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/Log.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/LogFactory.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/MimeBoundaryInputStream.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/MimeStreamParser.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/RootInputStream.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/codec/EncoderUtil.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/decoder/Base64InputStream.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/decoder/ByteQueue.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/decoder/DecoderUtil.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/decoder/QuotedPrintableInputStream.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/decoder/UnboundedFifoByteBuffer.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/AddressListField.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/ContentTransferEncodingField.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/ContentTypeField.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/DateTimeField.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/DefaultFieldParser.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/DelegatingFieldParser.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/Field.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/FieldParser.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/MailboxField.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/MailboxListField.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/UnstructuredField.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/address/Address.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/address/AddressList.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/address/Builder.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/address/DomainList.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/address/Group.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/address/Mailbox.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/address/MailboxList.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/address/NamedMailbox.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/address/parser/ASTaddr_spec.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/address/parser/ASTaddress.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/address/parser/ASTaddress_list.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/address/parser/ASTangle_addr.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/address/parser/ASTdomain.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/address/parser/ASTgroup_body.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/address/parser/ASTlocal_part.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/address/parser/ASTmailbox.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/address/parser/ASTname_addr.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/address/parser/ASTphrase.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/address/parser/ASTroute.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/address/parser/AddressListParser.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/address/parser/AddressListParser.jj delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/address/parser/AddressListParserConstants.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/address/parser/AddressListParserTokenManager.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/address/parser/AddressListParserTreeConstants.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/address/parser/AddressListParserVisitor.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/address/parser/BaseNode.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/address/parser/JJTAddressListParserState.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/address/parser/Node.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/address/parser/ParseException.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/address/parser/SimpleCharStream.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/address/parser/SimpleNode.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/address/parser/Token.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/address/parser/TokenMgrError.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/contenttype/parser/ContentTypeParser.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/contenttype/parser/ContentTypeParserConstants.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/contenttype/parser/ContentTypeParserTokenManager.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/contenttype/parser/ParseException.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/contenttype/parser/SimpleCharStream.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/contenttype/parser/Token.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/contenttype/parser/TokenMgrError.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/datetime/DateTime.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/datetime/parser/DateTimeParser.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/datetime/parser/DateTimeParserConstants.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/datetime/parser/DateTimeParserTokenManager.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/datetime/parser/ParseException.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/datetime/parser/SimpleCharStream.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/datetime/parser/Token.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/field/datetime/parser/TokenMgrError.java delete mode 100644 java/com/android/voicemailomtp/src/org/apache/james/mime4j/util/CharsetUtil.java delete mode 100644 java/com/android/voicemailomtp/sync/OmtpVvmSourceManager.java delete mode 100644 java/com/android/voicemailomtp/sync/OmtpVvmSyncReceiver.java delete mode 100644 java/com/android/voicemailomtp/sync/OmtpVvmSyncService.java delete mode 100644 java/com/android/voicemailomtp/sync/SyncOneTask.java delete mode 100644 java/com/android/voicemailomtp/sync/SyncTask.java delete mode 100644 java/com/android/voicemailomtp/sync/UploadTask.java delete mode 100644 java/com/android/voicemailomtp/sync/VoicemailProviderChangeReceiver.java delete mode 100644 java/com/android/voicemailomtp/sync/VoicemailStatusQueryHelper.java delete mode 100644 java/com/android/voicemailomtp/sync/VoicemailsQueryHelper.java delete mode 100644 java/com/android/voicemailomtp/sync/VvmNetworkRequest.java delete mode 100644 java/com/android/voicemailomtp/sync/VvmNetworkRequestCallback.java delete mode 100644 java/com/android/voicemailomtp/utils/IndentingPrintWriter.java delete mode 100644 java/com/android/voicemailomtp/utils/VoicemailDatabaseUtil.java delete mode 100644 java/com/android/voicemailomtp/utils/VvmDumpHandler.java delete mode 100644 java/com/android/voicemailomtp/utils/XmlUtils.java (limited to 'java/com/android') diff --git a/java/com/android/contacts/common/ContactPhotoManager.java b/java/com/android/contacts/common/ContactPhotoManager.java index 834471047..0f65a6c56 100644 --- a/java/com/android/contacts/common/ContactPhotoManager.java +++ b/java/com/android/contacts/common/ContactPhotoManager.java @@ -40,6 +40,7 @@ public abstract class ContactPhotoManager implements ComponentCallbacks2 { public static final int TYPE_BUSINESS = LetterTileDrawable.TYPE_BUSINESS; public static final int TYPE_VOICEMAIL = LetterTileDrawable.TYPE_VOICEMAIL; public static final int TYPE_DEFAULT = LetterTileDrawable.TYPE_DEFAULT; + public static final int TYPE_GENERIC_AVATAR = LetterTileDrawable.TYPE_GENERIC_AVATAR; /** Scale and offset default constants used for default letter images */ public static final float SCALE_DEFAULT = 1.0f; diff --git a/java/com/android/contacts/common/lettertiles/LetterTileDrawable.java b/java/com/android/contacts/common/lettertiles/LetterTileDrawable.java index 7e1839c1e..ca12f1812 100644 --- a/java/com/android/contacts/common/lettertiles/LetterTileDrawable.java +++ b/java/com/android/contacts/common/lettertiles/LetterTileDrawable.java @@ -19,6 +19,7 @@ package com.android.contacts.common.lettertiles; import android.content.res.Resources; import android.content.res.TypedArray; import android.graphics.Bitmap; +import android.graphics.Bitmap.Config; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.ColorFilter; @@ -48,13 +49,18 @@ public class LetterTileDrawable extends Drawable { * #TYPE_BUSINESS}, and voicemail contacts should use {@link #TYPE_VOICEMAIL}. */ @Retention(RetentionPolicy.SOURCE) - @IntDef({TYPE_PERSON, TYPE_BUSINESS, TYPE_VOICEMAIL}) + @IntDef({TYPE_PERSON, TYPE_BUSINESS, TYPE_VOICEMAIL, TYPE_GENERIC_AVATAR}) public @interface ContactType {} /** Contact type constants */ public static final int TYPE_PERSON = 1; public static final int TYPE_BUSINESS = 2; public static final int TYPE_VOICEMAIL = 3; + /** + * A generic avatar that features the default icon, default color, and no letter. Useful for + * situations where a contact is anonymous. + */ + public static final int TYPE_GENERIC_AVATAR = 4; @ContactType public static final int TYPE_DEFAULT = TYPE_PERSON; /** @@ -87,7 +93,6 @@ public class LetterTileDrawable extends Drawable { private static Bitmap sDefaultPersonAvatar; private static Bitmap sDefaultBusinessAvatar; private static Bitmap sDefaultVoicemailAvatar; - private static final String TAG = LetterTileDrawable.class.getSimpleName(); private final Paint mPaint; private int mContactType = TYPE_DEFAULT; private float mScale = 1.0f; @@ -97,7 +102,7 @@ public class LetterTileDrawable extends Drawable { private int mColor; private Character mLetter = null; - private boolean mAvatarWasVoicemailOrBusiness = false; + @ContactType private int mAvatarType = TYPE_DEFAULT; private String mDisplayName; public LetterTileDrawable(final Resources res) { @@ -130,6 +135,7 @@ public class LetterTileDrawable extends Drawable { case TYPE_VOICEMAIL: return sDefaultVoicemailAvatar; case TYPE_PERSON: + case TYPE_GENERIC_AVATAR: default: return sDefaultPersonAvatar; } @@ -149,6 +155,14 @@ public class LetterTileDrawable extends Drawable { drawLetterTile(canvas); } + public Bitmap getBitmap(int width, int height) { + Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888); + this.setBounds(0, 0, width, height); + Canvas canvas = new Canvas(bitmap); + this.draw(canvas); + return bitmap; + } + /** * Draw the bitmap onto the canvas at the current bounds taking into account the current scale. */ @@ -231,7 +245,9 @@ public class LetterTileDrawable extends Drawable { /** Returns a deterministic color based on the provided contact identifier string. */ private int pickColor(final String identifier) { - if (TextUtils.isEmpty(identifier) || mContactType == TYPE_VOICEMAIL) { + if (mContactType == TYPE_VOICEMAIL + || mContactType == TYPE_BUSINESS + || TextUtils.isEmpty(identifier)) { return sDefaultColor; } // String.hashCode() implementation is not supposed to change across java versions, so @@ -329,6 +345,10 @@ public class LetterTileDrawable extends Drawable { return this; } + public boolean tileIsCircular() { + return this.mIsCircle; + } + /** * Creates a canonical letter tile for use across dialer fragments. * @@ -344,39 +364,37 @@ public class LetterTileDrawable extends Drawable { @Nullable final String identifierForTileColor, @Shape final int shape, final int contactType) { - setContactType(contactType); + + this.setIsCircular(shape == SHAPE_CIRCLE); + /** - * During hangup, we lose the call state for special types of contacts, like voicemail. To help - * callers avoid extraneous LetterTileDrawable allocations, we keep track of the special case - * until we encounter a new display name. + * We return quickly under the following conditions: 1. We are asked to draw a default tile, and + * no coloring information is provided, meaning no further initialization is necessary OR 2. + * We've already invoked this method before, set mDisplayName, and found that it has not + * changed. This is useful during events like hangup, when we lose the call state for special + * types of contacts, like voicemail. We keep track of the special case until we encounter a new + * display name. */ - if (contactType == TYPE_VOICEMAIL || contactType == TYPE_BUSINESS) { - this.mAvatarWasVoicemailOrBusiness = true; - } else if (displayName != null && !displayName.equals(mDisplayName)) { - this.mAvatarWasVoicemailOrBusiness = false; + if (contactType == TYPE_DEFAULT + && ((displayName == null && identifierForTileColor == null) + || (displayName != null && displayName.equals(mDisplayName)))) { + return this; } + this.mDisplayName = displayName; - if (shape == SHAPE_CIRCLE) { - this.setIsCircular(true); - } else { - this.setIsCircular(false); - } + this.mAvatarType = contactType; + setContactType(this.mAvatarType); - /** - * To preserve style, we don't use contactType to set the tile icon. In the future, when all - * callers surface this detail, we can use this to better style the tile icon. - */ - if (mAvatarWasVoicemailOrBusiness) { - this.setLetterAndColorFromContactDetails(null, displayName); - return this; + // Special contact types receive default color and no letter tile, but special iconography. + if (this.mAvatarType != TYPE_PERSON) { + this.setLetterAndColorFromContactDetails(null, null); } else { if (identifierForTileColor != null) { this.setLetterAndColorFromContactDetails(displayName, identifierForTileColor); - return this; } else { this.setLetterAndColorFromContactDetails(displayName, displayName); - return this; } } + return this; } } diff --git a/java/com/android/contacts/common/list/ContactEntryListFragment.java b/java/com/android/contacts/common/list/ContactEntryListFragment.java index a8d9b55ba..278175c0b 100644 --- a/java/com/android/contacts/common/list/ContactEntryListFragment.java +++ b/java/com/android/contacts/common/list/ContactEntryListFragment.java @@ -16,7 +16,6 @@ package com.android.contacts.common.list; -import android.app.Activity; import android.app.Fragment; import android.app.LoaderManager; import android.app.LoaderManager.LoaderCallbacks; @@ -29,8 +28,8 @@ import android.os.Handler; import android.os.Message; import android.os.Parcelable; import android.provider.ContactsContract.Directory; +import android.support.annotation.Nullable; import android.text.TextUtils; -import android.util.Log; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; @@ -42,12 +41,13 @@ import android.widget.AbsListView; import android.widget.AbsListView.OnScrollListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; -import android.widget.AdapterView.OnItemLongClickListener; import android.widget.ListView; import com.android.common.widget.CompositeCursorAdapter.Partition; import com.android.contacts.common.ContactPhotoManager; import com.android.contacts.common.preference.ContactsPreferences; import com.android.contacts.common.util.ContactListViewUtils; +import com.android.dialer.common.LogUtil; +import java.lang.ref.WeakReference; import java.util.Locale; /** Common base class for various contact-related list fragments. */ @@ -56,9 +56,7 @@ public abstract class ContactEntryListFragment { - private static final String TAG = "ContactEntryListFragment"; private static final String KEY_LIST_STATE = "liststate"; private static final String KEY_SECTION_HEADER_DISPLAY_ENABLED = "sectionHeaderDisplayEnabled"; private static final String KEY_PHOTO_LOADER_ENABLED = "photoLoaderEnabled"; @@ -130,15 +128,27 @@ public abstract class ContactEntryListFragment> contactEntryListFragmentRef; + + private DelayedDirectorySearchHandler(ContactEntryListFragment contactEntryListFragment) { + this.contactEntryListFragmentRef = new WeakReference<>(contactEntryListFragment); + } + + @Override + public void handleMessage(Message msg) { + ContactEntryListFragment contactEntryListFragment = contactEntryListFragmentRef.get(); + if (contactEntryListFragment == null) { + return; + } + if (msg.what == DIRECTORY_SEARCH_MESSAGE) { + contactEntryListFragment.loadDirectoryPartition(msg.arg1, (DirectoryPartition) msg.obj); + } + } + } + private ContactsPreferences.ChangeListener mPreferencesChangeListener = new ContactsPreferences.ChangeListener() { @Override @@ -148,6 +158,10 @@ public abstract class ContactEntryListFragment parent, View view, int position, long id) { - int adjPosition = position - mListView.getHeaderViewsCount(); - - if (adjPosition >= 0) { - return onItemLongClick(adjPosition, id); - } - return false; - } - private void hideSoftKeyboard() { // Hide soft keyboard, if visible InputMethodManager inputMethodManager = diff --git a/java/com/android/contacts/common/widget/SelectPhoneAccountDialogFragment.java b/java/com/android/contacts/common/widget/SelectPhoneAccountDialogFragment.java index 63f8ca580..8156d97cf 100644 --- a/java/com/android/contacts/common/widget/SelectPhoneAccountDialogFragment.java +++ b/java/com/android/contacts/common/widget/SelectPhoneAccountDialogFragment.java @@ -59,8 +59,6 @@ public class SelectPhoneAccountDialogFragment extends DialogFragment { private static final String ARG_LISTENER = "listener"; private static final String ARG_CALL_ID = "call_id"; - private int mTitleResId; - private boolean mCanSetDefault; private List mAccountHandles; private boolean mIsSelected; private boolean mIsDefaultChecked; @@ -126,8 +124,8 @@ public class SelectPhoneAccountDialogFragment extends DialogFragment { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { - mTitleResId = getArguments().getInt(ARG_TITLE_RES_ID); - mCanSetDefault = getArguments().getBoolean(ARG_CAN_SET_DEFAULT); + int titleResId = getArguments().getInt(ARG_TITLE_RES_ID); + boolean canSetDefault = getArguments().getBoolean(ARG_CAN_SET_DEFAULT); mAccountHandles = getArguments().getParcelableArrayList(ARG_ACCOUNT_HANDLES); mListener = getArguments().getParcelable(ARG_LISTENER); if (savedInstanceState != null) { @@ -167,11 +165,11 @@ public class SelectPhoneAccountDialogFragment extends DialogFragment { AlertDialog dialog = builder - .setTitle(mTitleResId) + .setTitle(titleResId) .setAdapter(selectAccountListAdapter, selectionListener) .create(); - if (mCanSetDefault) { + if (canSetDefault) { // Generate custom checkbox view, lint suppressed since no appropriate parent (is dialog) @SuppressLint("InflateParams") LinearLayout checkboxLayout = @@ -190,13 +188,13 @@ public class SelectPhoneAccountDialogFragment extends DialogFragment { } @Override - public void onStop() { + public void onCancel(DialogInterface dialog) { if (!mIsSelected && mListener != null) { Bundle result = new Bundle(); result.putString(SelectPhoneAccountListener.EXTRA_CALL_ID, getCallId()); mListener.onReceiveResult(SelectPhoneAccountListener.RESULT_DISMISSED, result); } - super.onStop(); + super.onCancel(dialog); } @Nullable @@ -213,7 +211,7 @@ public class SelectPhoneAccountDialogFragment extends DialogFragment { static final String EXTRA_SET_DEFAULT = "extra_set_default"; static final String EXTRA_CALL_ID = "extra_call_id"; - public SelectPhoneAccountListener() { + protected SelectPhoneAccountListener() { super(new Handler()); } @@ -239,7 +237,7 @@ public class SelectPhoneAccountDialogFragment extends DialogFragment { private int mResId; - public SelectAccountListAdapter( + SelectAccountListAdapter( Context context, int resource, List accountHandles) { super(context, resource, accountHandles); mResId = resource; diff --git a/java/com/android/dialer/app/AndroidManifest.xml b/java/com/android/dialer/app/AndroidManifest.xml index 80f294acc..5ce13dbd7 100644 --- a/java/com/android/dialer/app/AndroidManifest.xml +++ b/java/com/android/dialer/app/AndroidManifest.xml @@ -57,11 +57,7 @@ android:minSdkVersion="23" android:targetSdkVersion="25"/> - + + + + diff --git a/java/com/android/dialer/app/CallDetailActivity.java b/java/com/android/dialer/app/CallDetailActivity.java deleted file mode 100644 index cda2b2e2c..000000000 --- a/java/com/android/dialer/app/CallDetailActivity.java +++ /dev/null @@ -1,480 +0,0 @@ -/* - * Copyright (C) 2009 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.app; - -import android.content.ContentUris; -import android.content.Context; -import android.content.Intent; -import android.content.res.Resources; -import android.net.Uri; -import android.os.AsyncTask; -import android.os.Bundle; -import android.provider.ContactsContract.CommonDataKinds.Phone; -import android.support.v7.app.AppCompatActivity; -import android.text.BidiFormatter; -import android.text.TextDirectionHeuristics; -import android.text.TextUtils; -import android.view.LayoutInflater; -import android.view.Menu; -import android.view.MenuItem; -import android.view.MotionEvent; -import android.view.View; -import android.widget.ListView; -import android.widget.QuickContactBadge; -import android.widget.TextView; -import android.widget.Toast; -import com.android.contacts.common.ClipboardUtils; -import com.android.contacts.common.ContactPhotoManager; -import com.android.contacts.common.ContactPhotoManager.DefaultImageRequest; -import com.android.contacts.common.GeoUtil; -import com.android.contacts.common.preference.ContactsPreferences; -import com.android.contacts.common.util.UriUtils; -import com.android.dialer.app.calllog.CallDetailHistoryAdapter; -import com.android.dialer.app.calllog.CallLogAsyncTaskUtil; -import com.android.dialer.app.calllog.CallLogAsyncTaskUtil.CallLogAsyncTaskListener; -import com.android.dialer.app.calllog.CallTypeHelper; -import com.android.dialer.app.calllog.PhoneAccountUtils; -import com.android.dialer.blocking.FilteredNumberAsyncQueryHandler; -import com.android.dialer.callintent.CallIntentBuilder; -import com.android.dialer.callintent.nano.CallInitiationType; -import com.android.dialer.common.Assert; -import com.android.dialer.common.AsyncTaskExecutor; -import com.android.dialer.common.AsyncTaskExecutors; -import com.android.dialer.compat.CompatUtils; -import com.android.dialer.logging.Logger; -import com.android.dialer.logging.nano.DialerImpression; -import com.android.dialer.logging.nano.ScreenEvent; -import com.android.dialer.phonenumbercache.ContactInfoHelper; -import com.android.dialer.phonenumberutil.PhoneNumberHelper; -import com.android.dialer.proguard.UsedByReflection; -import com.android.dialer.spam.Spam; -import com.android.dialer.telecom.TelecomUtil; -import com.android.dialer.util.CallUtil; -import com.android.dialer.util.DialerUtils; -import com.android.dialer.util.TouchPointManager; - -/** - * Displays the details of a specific call log entry. - * - *

This activity can be either started with the URI of a single call log entry, or with the - * {@link #EXTRA_CALL_LOG_IDS} extra to specify a group of call log entries. - */ -@UsedByReflection(value = "AndroidManifest-app.xml") -public class CallDetailActivity extends AppCompatActivity - implements MenuItem.OnMenuItemClickListener, View.OnClickListener { - - /** A long array extra containing ids of call log entries to display. */ - public static final String EXTRA_CALL_LOG_IDS = "EXTRA_CALL_LOG_IDS"; - /** If we are started with a voicemail, we'll find the uri to play with this extra. */ - public static final String EXTRA_VOICEMAIL_URI = "EXTRA_VOICEMAIL_URI"; - /** If the activity was triggered from a notification. */ - public static final String EXTRA_FROM_NOTIFICATION = "EXTRA_FROM_NOTIFICATION"; - - public static final String BLOCKED_OR_SPAM_QUERY_IDENTIFIER = "blockedOrSpamIdentifier"; - - private final AsyncTaskExecutor executor = AsyncTaskExecutors.createAsyncTaskExecutor(); - protected String mNumber; - private Context mContext; - private ContactInfoHelper mContactInfoHelper; - private ContactsPreferences mContactsPreferences; - private CallTypeHelper mCallTypeHelper; - private ContactPhotoManager mContactPhotoManager; - private BidiFormatter mBidiFormatter = BidiFormatter.getInstance(); - private LayoutInflater mInflater; - private Resources mResources; - private PhoneCallDetails mDetails; - private Uri mVoicemailUri; - private String mPostDialDigits = ""; - private ListView mHistoryList; - private QuickContactBadge mQuickContactBadge; - private TextView mCallerName; - private TextView mCallerNumber; - private TextView mAccountLabel; - private View mCallButton; - private View mEditBeforeCallActionItem; - private View mReportActionItem; - private View mCopyNumberActionItem; - private FilteredNumberAsyncQueryHandler mFilteredNumberAsyncQueryHandler; - private CallLogAsyncTaskListener mCallLogAsyncTaskListener = - new CallLogAsyncTaskListener() { - @Override - public void onDeleteCall() { - finish(); - } - - @Override - public void onDeleteVoicemail() { - finish(); - } - - @Override - public void onGetCallDetails(final PhoneCallDetails[] details) { - if (details == null) { - // Somewhere went wrong: we're going to bail out and show error to users. - Toast.makeText(mContext, R.string.toast_call_detail_error, Toast.LENGTH_SHORT).show(); - finish(); - return; - } - - // All calls are from the same number and same contact, so pick the first detail. - mDetails = details[0]; - mNumber = TextUtils.isEmpty(mDetails.number) ? null : mDetails.number.toString(); - - if (mNumber == null) { - updateDataAndRender(details); - return; - } - - executor.submit( - BLOCKED_OR_SPAM_QUERY_IDENTIFIER, - new AsyncTask() { - @Override - protected Void doInBackground(Void... params) { - mDetails.isBlocked = - mFilteredNumberAsyncQueryHandler.getBlockedIdSynchronousForCalllogOnly( - mNumber, mDetails.countryIso) - != null; - if (Spam.get(mContext).isSpamEnabled()) { - mDetails.isSpam = - hasIncomingCalls(details) - && Spam.get(mContext) - .checkSpamStatusSynchronous(mNumber, mDetails.countryIso); - } - return null; - } - - @Override - protected void onPostExecute(Void result) { - updateDataAndRender(details); - } - }); - } - - private void updateDataAndRender(PhoneCallDetails[] details) { - mPostDialDigits = - TextUtils.isEmpty(mDetails.postDialDigits) ? "" : mDetails.postDialDigits; - - final CharSequence callLocationOrType = getNumberTypeOrLocation(mDetails); - - final CharSequence displayNumber; - if (!TextUtils.isEmpty(mDetails.postDialDigits)) { - displayNumber = mDetails.number + mDetails.postDialDigits; - } else { - displayNumber = mDetails.displayNumber; - } - - final String displayNumberStr = - mBidiFormatter.unicodeWrap(displayNumber.toString(), TextDirectionHeuristics.LTR); - - mDetails.nameDisplayOrder = mContactsPreferences.getDisplayOrder(); - - if (!TextUtils.isEmpty(mDetails.getPreferredName())) { - mCallerName.setText(mDetails.getPreferredName()); - mCallerNumber.setText(callLocationOrType + " " + displayNumberStr); - } else { - mCallerName.setText(displayNumberStr); - if (!TextUtils.isEmpty(callLocationOrType)) { - mCallerNumber.setText(callLocationOrType); - mCallerNumber.setVisibility(View.VISIBLE); - } else { - mCallerNumber.setVisibility(View.GONE); - } - } - - CharSequence accountLabel = - PhoneAccountUtils.getAccountLabel(mContext, mDetails.accountHandle); - CharSequence accountContentDescription = - PhoneCallDetails.createAccountLabelDescription( - mResources, mDetails.viaNumber, accountLabel); - if (!TextUtils.isEmpty(mDetails.viaNumber)) { - if (!TextUtils.isEmpty(accountLabel)) { - accountLabel = - mResources.getString( - R.string.call_log_via_number_phone_account, accountLabel, mDetails.viaNumber); - } else { - accountLabel = mResources.getString(R.string.call_log_via_number, mDetails.viaNumber); - } - } - if (!TextUtils.isEmpty(accountLabel)) { - mAccountLabel.setText(accountLabel); - mAccountLabel.setContentDescription(accountContentDescription); - mAccountLabel.setVisibility(View.VISIBLE); - } else { - mAccountLabel.setVisibility(View.GONE); - } - - final boolean canPlaceCallsTo = - PhoneNumberHelper.canPlaceCallsTo(mNumber, mDetails.numberPresentation); - mCallButton.setVisibility(canPlaceCallsTo ? View.VISIBLE : View.GONE); - mCopyNumberActionItem.setVisibility(canPlaceCallsTo ? View.VISIBLE : View.GONE); - - final boolean isSipNumber = PhoneNumberHelper.isSipNumber(mNumber); - final boolean isVoicemailNumber = - PhoneNumberHelper.isVoicemailNumber(mContext, mDetails.accountHandle, mNumber); - final boolean showEditNumberBeforeCallAction = - canPlaceCallsTo && !isSipNumber && !isVoicemailNumber; - mEditBeforeCallActionItem.setVisibility( - showEditNumberBeforeCallAction ? View.VISIBLE : View.GONE); - - final boolean showReportAction = - mContactInfoHelper.canReportAsInvalid(mDetails.sourceType, mDetails.objectId); - mReportActionItem.setVisibility(showReportAction ? View.VISIBLE : View.GONE); - - invalidateOptionsMenu(); - - mHistoryList.setAdapter( - new CallDetailHistoryAdapter(mContext, mInflater, mCallTypeHelper, details)); - - updateContactPhoto(mDetails.isSpam); - - findViewById(R.id.call_detail).setVisibility(View.VISIBLE); - } - - /** - * Determines the location geocode text for a call, or the phone number type (if available). - * - * @param details The call details. - * @return The phone number type or location. - */ - private CharSequence getNumberTypeOrLocation(PhoneCallDetails details) { - if (details.isSpam) { - return mResources.getString(R.string.spam_number_call_log_label); - } else if (details.isBlocked) { - return mResources.getString(R.string.blocked_number_call_log_label); - } else if (!TextUtils.isEmpty(details.namePrimary)) { - return Phone.getTypeLabel(mResources, details.numberType, details.numberLabel); - } else { - return details.geocode; - } - } - }; - - @Override - protected void onCreate(Bundle icicle) { - super.onCreate(icicle); - - mContext = this; - mResources = getResources(); - mContactInfoHelper = new ContactInfoHelper(this, GeoUtil.getCurrentCountryIso(this)); - mContactsPreferences = new ContactsPreferences(mContext); - mCallTypeHelper = new CallTypeHelper(getResources()); - mFilteredNumberAsyncQueryHandler = new FilteredNumberAsyncQueryHandler(mContext); - - mVoicemailUri = getIntent().getParcelableExtra(EXTRA_VOICEMAIL_URI); - - getSupportActionBar().setDisplayHomeAsUpEnabled(true); - - setContentView(R.layout.call_detail); - mInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); - - mHistoryList = (ListView) findViewById(R.id.history); - mHistoryList.addHeaderView(mInflater.inflate(R.layout.call_detail_header, null)); - mHistoryList.addFooterView(mInflater.inflate(R.layout.call_detail_footer, null), null, false); - - mQuickContactBadge = (QuickContactBadge) findViewById(R.id.quick_contact_photo); - mQuickContactBadge.setOverlay(null); - if (CompatUtils.hasPrioritizedMimeType()) { - mQuickContactBadge.setPrioritizedMimeType(Phone.CONTENT_ITEM_TYPE); - } - mCallerName = (TextView) findViewById(R.id.caller_name); - mCallerNumber = (TextView) findViewById(R.id.caller_number); - mAccountLabel = (TextView) findViewById(R.id.phone_account_label); - mContactPhotoManager = ContactPhotoManager.getInstance(this); - - mCallButton = findViewById(R.id.call_back_button); - mCallButton.setOnClickListener( - new View.OnClickListener() { - @Override - public void onClick(View view) { - if (TextUtils.isEmpty(mNumber)) { - return; - } - DialerUtils.startActivityWithErrorToast( - CallDetailActivity.this, - new CallIntentBuilder(getDialableNumber(), CallInitiationType.Type.CALL_DETAILS) - .build()); - } - }); - - mEditBeforeCallActionItem = findViewById(R.id.call_detail_action_edit_before_call); - mEditBeforeCallActionItem.setOnClickListener(this); - mReportActionItem = findViewById(R.id.call_detail_action_report); - mReportActionItem.setOnClickListener(this); - - mCopyNumberActionItem = findViewById(R.id.call_detail_action_copy); - mCopyNumberActionItem.setOnClickListener(this); - - if (getIntent().getBooleanExtra(EXTRA_FROM_NOTIFICATION, false)) { - closeSystemDialogs(); - } - - Logger.get(this).logScreenView(ScreenEvent.Type.CALL_DETAILS, this); - } - - @Override - public void onResume() { - super.onResume(); - mContactsPreferences.refreshValue(ContactsPreferences.DISPLAY_ORDER_KEY); - getCallDetails(); - } - - @Override - public boolean dispatchTouchEvent(MotionEvent ev) { - if (ev.getAction() == MotionEvent.ACTION_DOWN) { - TouchPointManager.getInstance().setPoint((int) ev.getRawX(), (int) ev.getRawY()); - } - return super.dispatchTouchEvent(ev); - } - - public void getCallDetails() { - CallLogAsyncTaskUtil.getCallDetails(this, mCallLogAsyncTaskListener, getCallLogEntryUris()); - } - - /** - * Returns the list of URIs to show. - * - *

There are two ways the URIs can be provided to the activity: as the data on the intent, or - * as a list of ids in the call log added as an extra on the URI. - * - *

If both are available, the data on the intent takes precedence. - */ - private Uri[] getCallLogEntryUris() { - final Uri uri = getIntent().getData(); - if (uri != null) { - // If there is a data on the intent, it takes precedence over the extra. - return new Uri[] {uri}; - } - final long[] ids = getIntent().getLongArrayExtra(EXTRA_CALL_LOG_IDS); - final int numIds = ids == null ? 0 : ids.length; - final Uri[] uris = new Uri[numIds]; - for (int index = 0; index < numIds; ++index) { - uris[index] = - ContentUris.withAppendedId( - TelecomUtil.getCallLogUri(CallDetailActivity.this), ids[index]); - } - return uris; - } - - @Override - public boolean onCreateOptionsMenu(Menu menu) { - final MenuItem deleteMenuItem = - menu.add( - Menu.NONE, R.id.call_detail_delete_menu_item, Menu.NONE, R.string.call_details_delete); - deleteMenuItem.setIcon(R.drawable.ic_delete_24dp); - deleteMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); - deleteMenuItem.setOnMenuItemClickListener(this); - - return super.onCreateOptionsMenu(menu); - } - - @Override - public boolean onMenuItemClick(MenuItem item) { - if (item.getItemId() == R.id.call_detail_delete_menu_item) { - Logger.get(mContext).logImpression(DialerImpression.Type.USER_DELETED_CALL_LOG_ITEM); - if (hasVoicemail()) { - CallLogAsyncTaskUtil.deleteVoicemail(this, mVoicemailUri, mCallLogAsyncTaskListener); - } else { - final StringBuilder callIds = new StringBuilder(); - for (Uri callUri : getCallLogEntryUris()) { - if (callIds.length() != 0) { - callIds.append(","); - } - callIds.append(ContentUris.parseId(callUri)); - } - CallLogAsyncTaskUtil.deleteCalls(this, callIds.toString(), mCallLogAsyncTaskListener); - } - } - return true; - } - - @Override - public void onClick(View view) { - int resId = view.getId(); - if (resId == R.id.call_detail_action_copy) { - ClipboardUtils.copyText(mContext, null, mNumber, true); - } else if (resId == R.id.call_detail_action_edit_before_call) { - Intent dialIntent = new Intent(Intent.ACTION_DIAL, CallUtil.getCallUri(getDialableNumber())); - DialerUtils.startActivityWithErrorToast(mContext, dialIntent); - } else { - Assert.fail("Unexpected onClick event from " + view); - } - } - - // Loads and displays the contact photo. - private void updateContactPhoto(boolean isSpam) { - if (mDetails == null) { - return; - } - - mQuickContactBadge.assignContactUri(mDetails.contactUri); - final String displayName = - TextUtils.isEmpty(mDetails.namePrimary) - ? mDetails.displayNumber - : mDetails.namePrimary.toString(); - mQuickContactBadge.setContentDescription( - mResources.getString(R.string.description_contact_details, displayName)); - - final boolean isVoicemailNumber = - PhoneNumberHelper.isVoicemailNumber(mContext, mDetails.accountHandle, mNumber); - if (isSpam) { - mQuickContactBadge.setImageDrawable(mContext.getDrawable(R.drawable.blocked_contact)); - return; - } - - final boolean isBusiness = mContactInfoHelper.isBusiness(mDetails.sourceType); - int contactType = ContactPhotoManager.TYPE_DEFAULT; - if (isVoicemailNumber) { - contactType = ContactPhotoManager.TYPE_VOICEMAIL; - } else if (isBusiness) { - contactType = ContactPhotoManager.TYPE_BUSINESS; - } - - final String lookupKey = - mDetails.contactUri == null ? null : UriUtils.getLookupKeyFromUri(mDetails.contactUri); - - final DefaultImageRequest request = - new DefaultImageRequest(displayName, lookupKey, contactType, true /* isCircular */); - - mContactPhotoManager.loadDirectoryPhoto( - mQuickContactBadge, - mDetails.photoUri, - false /* darkTheme */, - true /* isCircular */, - request); - } - - private void closeSystemDialogs() { - sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)); - } - - private String getDialableNumber() { - return mNumber + mPostDialDigits; - } - - public boolean hasVoicemail() { - return mVoicemailUri != null; - } - - private static boolean hasIncomingCalls(PhoneCallDetails[] details) { - for (int i = 0; i < details.length; i++) { - if (details[i].hasIncomingCalls()) { - return true; - } - } - return false; - } -} diff --git a/java/com/android/dialer/app/DialerApplication.java b/java/com/android/dialer/app/DialerApplication.java deleted file mode 100644 index 3b979212b..000000000 --- a/java/com/android/dialer/app/DialerApplication.java +++ /dev/null @@ -1,77 +0,0 @@ -/* - * Copyright (C) 2013 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.app; - -import android.app.Application; -import android.os.Trace; -import android.preference.PreferenceManager; -import com.android.dialer.blocking.BlockedNumbersAutoMigrator; -import com.android.dialer.blocking.FilteredNumberAsyncQueryHandler; -import com.android.dialer.enrichedcall.EnrichedCallManager; -import com.android.dialer.inject.ApplicationModule; -import com.android.dialer.inject.DaggerDialerAppComponent; -import com.android.dialer.inject.DialerAppComponent; - -public class DialerApplication extends Application implements EnrichedCallManager.Factory { - - private static final String TAG = "DialerApplication"; - - private volatile DialerAppComponent component; - - @Override - public void onCreate() { - Trace.beginSection(TAG + " onCreate"); - super.onCreate(); - new BlockedNumbersAutoMigrator( - this, - PreferenceManager.getDefaultSharedPreferences(this), - new FilteredNumberAsyncQueryHandler(this)) - .autoMigrate(); - Trace.endSection(); - } - - @Override - public EnrichedCallManager getEnrichedCallManager() { - return component().enrichedCallManager(); - } - - protected DialerAppComponent buildApplicationComponent() { - return DaggerDialerAppComponent.builder() - .applicationModule(new ApplicationModule(this)) - .build(); - } - - /** - * Returns the application component. - * - *

A single Component is created per application instance. Note that it won't be instantiated - * until it's first requested, but guarantees that only one will ever be created. - */ - private final DialerAppComponent component() { - // Double-check idiom for lazy initialization - DialerAppComponent result = component; - if (result == null) { - synchronized (this) { - result = component; - if (result == null) { - component = result = buildApplicationComponent(); - } - } - } - return result; - } -} diff --git a/java/com/android/dialer/app/DialtactsActivity.java b/java/com/android/dialer/app/DialtactsActivity.java index 4c57cda70..b2837769f 100644 --- a/java/com/android/dialer/app/DialtactsActivity.java +++ b/java/com/android/dialer/app/DialtactsActivity.java @@ -63,15 +63,14 @@ import android.widget.Toast; import com.android.contacts.common.dialog.ClearFrequentsDialog; import com.android.contacts.common.list.OnPhoneNumberPickerActionListener; import com.android.contacts.common.list.PhoneNumberListAdapter; -import com.android.contacts.common.list.PhoneNumberListAdapter.PhoneQuery; import com.android.contacts.common.list.PhoneNumberPickerFragment.CursorReranker; import com.android.contacts.common.list.PhoneNumberPickerFragment.OnLoadFinishedListener; import com.android.contacts.common.widget.FloatingActionButtonController; import com.android.dialer.animation.AnimUtils; import com.android.dialer.animation.AnimationListenerAdapter; +import com.android.dialer.app.calllog.CallLogActivity; import com.android.dialer.app.calllog.CallLogFragment; import com.android.dialer.app.calllog.CallLogNotificationsService; -import com.android.dialer.app.calllog.ClearCallLogDialog; import com.android.dialer.app.dialpad.DialpadFragment; import com.android.dialer.app.list.DragDropController; import com.android.dialer.app.list.ListsFragment; @@ -85,6 +84,7 @@ import com.android.dialer.app.list.SpeedDialFragment; import com.android.dialer.app.settings.DialerSettingsActivity; import com.android.dialer.app.widget.ActionBarController; import com.android.dialer.app.widget.SearchEditTextLayout; +import com.android.dialer.callcomposer.CallComposerActivity; import com.android.dialer.callintent.CallIntentBuilder; import com.android.dialer.callintent.nano.CallSpecificAppData; import com.android.dialer.common.Assert; @@ -101,7 +101,10 @@ import com.android.dialer.p13n.inference.protocol.P13nRanker; import com.android.dialer.p13n.inference.protocol.P13nRanker.P13nRefreshCompleteListener; import com.android.dialer.p13n.logging.P13nLogger; import com.android.dialer.p13n.logging.P13nLogging; +import com.android.dialer.postcall.PostCall; import com.android.dialer.proguard.UsedByReflection; +import com.android.dialer.simulator.Simulator; +import com.android.dialer.simulator.SimulatorComponent; import com.android.dialer.smartdial.SmartDialNameMatcher; import com.android.dialer.smartdial.SmartDialPrefix; import com.android.dialer.telecom.TelecomUtil; @@ -124,7 +127,6 @@ public class DialtactsActivity extends TransactionSafeActivity OnListFragmentScrolledListener, CallLogFragment.HostInterface, DialpadFragment.HostInterface, - ListsFragment.HostInterface, SpeedDialFragment.HostInterface, SearchFragment.HostInterface, OnDragDropListener, @@ -478,6 +480,7 @@ public class DialtactsActivity extends TransactionSafeActivity @Override protected void onResume() { + LogUtil.d("DialtactsActivity.onResume", ""); Trace.beginSection(TAG + " onResume"); super.onResume(); @@ -490,6 +493,8 @@ public class DialtactsActivity extends TransactionSafeActivity } else if (mShowDialpadOnResume) { showDialpadFragment(false); mShowDialpadOnResume = false; + } else { + PostCall.promptUserForMessageIfNecessary(this, mParentLayout); } // If there was a voice query result returned in the {@link #onActivityResult} callback, it @@ -539,7 +544,7 @@ public class DialtactsActivity extends TransactionSafeActivity } if (getIntent().getBooleanExtra(EXTRA_CLEAR_NEW_VOICEMAILS, false)) { - CallLogNotificationsService.markNewVoicemailsAsOld(this); + CallLogNotificationsService.markNewVoicemailsAsOld(this, null); } setSearchBoxHint(); @@ -588,6 +593,7 @@ public class DialtactsActivity extends TransactionSafeActivity @Override public void onAttachFragment(final Fragment fragment) { + LogUtil.d("DialtactsActivity.onAttachFragment", "fragment: %s", fragment); if (fragment instanceof DialpadFragment) { mDialpadFragment = (DialpadFragment) fragment; if (!mIsDialpadShown && !mShowDialpadOnResume) { @@ -616,7 +622,8 @@ public class DialtactsActivity extends TransactionSafeActivity @MainThread public Cursor rerankCursor(Cursor data) { Assert.isMainThread(); - return mP13nRanker.rankCursor(data, PhoneQuery.PHONE_NUMBER); + String queryString = searchFragment.getQueryString(); + return mP13nRanker.rankCursor(data, queryString == null ? 0 : queryString.length()); } }); searchFragment.addOnLoadFinishedListener( @@ -674,9 +681,9 @@ public class DialtactsActivity extends TransactionSafeActivity } int resId = item.getItemId(); - if (item.getItemId() == R.id.menu_delete_all) { - ClearCallLogDialog.show(getFragmentManager()); - return true; + if (resId == R.id.menu_history) { + final Intent intent = new Intent(this, CallLogActivity.class); + startActivity(intent); } else if (resId == R.id.menu_clear_frequents) { ClearFrequentsDialog.show(getFragmentManager()); Logger.get(this).logScreenView(ScreenEvent.Type.CLEAR_FREQUENTS, this); @@ -691,6 +698,11 @@ public class DialtactsActivity extends TransactionSafeActivity @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { + LogUtil.i( + "DialtactsActivity.onActivityResult", + "requestCode:%d, resultCode:%d", + requestCode, + resultCode); if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) { if (resultCode == RESULT_OK) { final ArrayList matches = @@ -701,15 +713,16 @@ public class DialtactsActivity extends TransactionSafeActivity LogUtil.i("DialtactsActivity.onActivityResult", "voice search - nothing heard"); } } else { - LogUtil.e("DialtactsActivity.onActivityResult", "voice search failed: " + resultCode); + LogUtil.e("DialtactsActivity.onActivityResult", "voice search failed"); } } else if (requestCode == ACTIVITY_REQUEST_CODE_CALL_COMPOSE) { - if (resultCode != RESULT_OK) { + if (resultCode == RESULT_FIRST_USER) { LogUtil.i( - "DialtactsActivity.onActivityResult", - "returned from call composer, error occurred (resultCode=" + resultCode + ")"); + "DialtactsActivity.onActivityResult", "returned from call composer, error occurred"); String message = - getString(R.string.call_composer_connection_failed, getString(R.string.share_and_call)); + getString( + R.string.call_composer_connection_failed, + data.getStringExtra(CallComposerActivity.KEY_CONTACT_NAME)); Snackbar.make(mParentLayout, message, Snackbar.LENGTH_LONG).show(); } else { LogUtil.i("DialtactsActivity.onActivityResult", "returned from call composer, no error"); @@ -732,6 +745,7 @@ public class DialtactsActivity extends TransactionSafeActivity * @see #onDialpadShown */ private void showDialpadFragment(boolean animate) { + LogUtil.d("DialtactActivity.showDialpadFragment", "animate: %b", animate); if (mIsDialpadShown || mStateSaved) { return; } @@ -767,6 +781,7 @@ public class DialtactsActivity extends TransactionSafeActivity /** Callback from child DialpadFragment when the dialpad is shown. */ public void onDialpadShown() { + LogUtil.d("DialtactsActivity.onDialpadShown", ""); Assert.isNotNull(mDialpadFragment); if (mDialpadFragment.getAnimate()) { Assert.isNotNull(mDialpadFragment.getView()).startAnimation(mSlideIn); @@ -838,12 +853,21 @@ public class DialtactsActivity extends TransactionSafeActivity private void updateSearchFragmentPosition() { SearchFragment fragment = null; - if (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()) { + if (mSmartDialSearchFragment != null) { fragment = mSmartDialSearchFragment; - } else if (mRegularSearchFragment != null && mRegularSearchFragment.isVisible()) { + } else if (mRegularSearchFragment != null) { fragment = mRegularSearchFragment; } - if (fragment != null && fragment.isVisible()) { + LogUtil.d( + "DialtactsActivity.updateSearchFragmentPosition", + "fragment: %s, isVisible: %b", + fragment, + fragment != null && fragment.isVisible()); + if (fragment != null) { + // We need to force animation here even when fragment is not visible since it might not be + // visible immediately after screen orientation change and dialpad height would not be + // available immediately which is required to update position. By forcing an animation, + // position will be updated after a delay by when the dialpad height would be available. fragment.updatePosition(true /* animate */); } } @@ -858,11 +882,6 @@ public class DialtactsActivity extends TransactionSafeActivity return !TextUtils.isEmpty(mSearchQuery); } - @Override - public boolean shouldShowActionBar() { - return mListsFragment.shouldShowActionBar(); - } - private void setNotInSearchUi() { mInDialpadSearch = false; mInRegularSearch = false; @@ -1056,7 +1075,8 @@ public class DialtactsActivity extends TransactionSafeActivity } // DialtactsActivity will provide the options menu fragment.setHasOptionsMenu(false); - fragment.setShowEmptyListForNullQuery(true); + // Will show empty list if P13nRanker is not enabled. Else, re-ranked list by the ranker. + fragment.setShowEmptyListForNullQuery(mP13nRanker.shouldShowEmptyListForNullQuery()); if (!smartDialSearch) { fragment.setQueryString(query); } @@ -1360,11 +1380,6 @@ public class DialtactsActivity extends TransactionSafeActivity return mActionBarController.isActionBarShowing(); } - @Override - public ActionBarController getActionBarController() { - return mActionBarController; - } - @Override public boolean isDialpadShown() { return mIsDialpadShown; @@ -1378,11 +1393,6 @@ public class DialtactsActivity extends TransactionSafeActivity return 0; } - @Override - public int getActionBarHideOffset() { - return getActionBarSafely().getHideOffset(); - } - @Override public void setActionBarHideOffset(int offset) { getActionBarSafely().setHideOffset(offset); @@ -1461,8 +1471,19 @@ public class DialtactsActivity extends TransactionSafeActivity && mListsFragment.getSpeedDialFragment().hasFrequents() && hasContactsPermission); - menu.findItem(R.id.menu_delete_all) + menu.findItem(R.id.menu_history) .setVisible(PermissionsUtil.hasPhonePermissions(DialtactsActivity.this)); + + Context context = DialtactsActivity.this.getApplicationContext(); + MenuItem simulatorMenuItem = menu.findItem(R.id.menu_simulator_submenu); + Simulator simulator = SimulatorComponent.get(context).getSimulator(); + if (simulator.shouldShow()) { + simulatorMenuItem.setVisible(true); + simulatorMenuItem.setActionProvider(simulator.getActionProvider(context)); + } else { + simulatorMenuItem.setVisible(false); + } + super.show(); } } diff --git a/java/com/android/dialer/app/PhoneCallDetails.java b/java/com/android/dialer/app/PhoneCallDetails.java deleted file mode 100644 index 436f68eec..000000000 --- a/java/com/android/dialer/app/PhoneCallDetails.java +++ /dev/null @@ -1,207 +0,0 @@ -/* - * Copyright (C) 2011 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.app; - -import android.content.Context; -import android.content.res.Resources; -import android.net.Uri; -import android.provider.CallLog; -import android.provider.CallLog.Calls; -import android.support.annotation.Nullable; -import android.telecom.PhoneAccountHandle; -import android.text.TextUtils; -import com.android.contacts.common.ContactsUtils.UserType; -import com.android.contacts.common.preference.ContactsPreferences; -import com.android.contacts.common.util.ContactDisplayUtils; -import com.android.dialer.app.calllog.PhoneNumberDisplayUtil; -import com.android.dialer.phonenumbercache.ContactInfo; - -/** The details of a phone call to be shown in the UI. */ -public class PhoneCallDetails { - - // The number of the other party involved in the call. - public CharSequence number; - // Post-dial digits associated with the outgoing call. - public String postDialDigits; - // The secondary line number the call was received via. - public String viaNumber; - // The number presenting rules set by the network, e.g., {@link Calls#PRESENTATION_ALLOWED} - public int numberPresentation; - // The country corresponding with the phone number. - public String countryIso; - // The geocoded location for the phone number. - public String geocode; - - /** - * The type of calls, as defined in the call log table, e.g., {@link Calls#INCOMING_TYPE}. - * - *

There might be multiple types if this represents a set of entries grouped together. - */ - public int[] callTypes; - - // The date of the call, in milliseconds since the epoch. - public long date; - // The duration of the call in milliseconds, or 0 for missed calls. - public long duration; - // The name of the contact, or the empty string. - public CharSequence namePrimary; - // The alternative name of the contact, e.g. last name first, or the empty string - public CharSequence nameAlternative; - /** - * The user's preference on name display order, last name first or first time first. {@see - * ContactsPreferences} - */ - public int nameDisplayOrder; - // The type of phone, e.g., {@link Phone#TYPE_HOME}, 0 if not available. - public int numberType; - // The custom label associated with the phone number in the contact, or the empty string. - public CharSequence numberLabel; - // The URI of the contact associated with this phone call. - public Uri contactUri; - - /** - * The photo URI of the picture of the contact that is associated with this phone call or null if - * there is none. - * - *

This is meant to store the high-res photo only. - */ - public Uri photoUri; - - // The source type of the contact associated with this call. - public int sourceType; - - // The object id type of the contact associated with this call. - public String objectId; - - // The unique identifier for the account associated with the call. - public PhoneAccountHandle accountHandle; - - // Features applicable to this call. - public int features; - - // Total data usage for this call. - public Long dataUsage; - - // Voicemail transcription - public String transcription; - - // The display string for the number. - public String displayNumber; - - // Whether the contact number is a voicemail number. - public boolean isVoicemail; - - /** The {@link UserType} of the contact */ - public @UserType long contactUserType; - - /** - * If this is a voicemail, whether the message is read. For other types of calls, this defaults to - * {@code true}. - */ - public boolean isRead = true; - - // If this call is a spam number. - public boolean isSpam = false; - - // If this call is a blocked number. - public boolean isBlocked = false; - - // Call location and date text. - public CharSequence callLocationAndDate; - - // Call description. - public CharSequence callDescription; - public String accountComponentName; - public String accountId; - public ContactInfo cachedContactInfo; - public int voicemailId; - public int previousGroup; - - /** - * Constructor with required fields for the details of a call with a number associated with a - * contact. - */ - public PhoneCallDetails( - CharSequence number, int numberPresentation, CharSequence postDialDigits) { - this.number = number; - this.numberPresentation = numberPresentation; - this.postDialDigits = postDialDigits.toString(); - } - /** - * Construct the "on {accountLabel} via {viaNumber}" accessibility description for the account - * list item, depending on the existence of the accountLabel and viaNumber. - * - * @param viaNumber The number that this call is being placed via. - * @param accountLabel The {@link PhoneAccount} label that this call is being placed with. - * @return The description of the account that this call has been placed on. - */ - public static CharSequence createAccountLabelDescription( - Resources resources, @Nullable String viaNumber, @Nullable CharSequence accountLabel) { - - if ((!TextUtils.isEmpty(viaNumber)) && !TextUtils.isEmpty(accountLabel)) { - String msg = - resources.getString( - R.string.description_via_number_phone_account, accountLabel, viaNumber); - CharSequence accountNumberLabel = - ContactDisplayUtils.getTelephoneTtsSpannable(msg, viaNumber); - return (accountNumberLabel == null) ? msg : accountNumberLabel; - } else if (!TextUtils.isEmpty(viaNumber)) { - CharSequence viaNumberLabel = - ContactDisplayUtils.getTtsSpannedPhoneNumber( - resources, R.string.description_via_number, viaNumber); - return (viaNumberLabel == null) ? viaNumber : viaNumberLabel; - } else if (!TextUtils.isEmpty(accountLabel)) { - return TextUtils.expandTemplate( - resources.getString(R.string.description_phone_account), accountLabel); - } - return ""; - } - - /** - * Returns the preferred name for the call details as specified by the {@link #nameDisplayOrder} - * - * @return the preferred name - */ - public CharSequence getPreferredName() { - if (nameDisplayOrder == ContactsPreferences.DISPLAY_ORDER_PRIMARY - || TextUtils.isEmpty(nameAlternative)) { - return namePrimary; - } - return nameAlternative; - } - - public void updateDisplayNumber( - Context context, CharSequence formattedNumber, boolean isVoicemail) { - displayNumber = - PhoneNumberDisplayUtil.getDisplayNumber( - context, number, numberPresentation, formattedNumber, postDialDigits, isVoicemail) - .toString(); - } - - public boolean hasIncomingCalls() { - for (int i = 0; i < callTypes.length; i++) { - if (callTypes[i] == CallLog.Calls.INCOMING_TYPE - || callTypes[i] == CallLog.Calls.MISSED_TYPE - || callTypes[i] == CallLog.Calls.VOICEMAIL_TYPE - || callTypes[i] == CallLog.Calls.REJECTED_TYPE - || callTypes[i] == CallLog.Calls.BLOCKED_TYPE) { - return true; - } - } - return false; - } -} diff --git a/java/com/android/dialer/app/SpecialCharSequenceMgr.java b/java/com/android/dialer/app/SpecialCharSequenceMgr.java index 2ae19704a..712659c12 100644 --- a/java/com/android/dialer/app/SpecialCharSequenceMgr.java +++ b/java/com/android/dialer/app/SpecialCharSequenceMgr.java @@ -28,15 +28,14 @@ import android.content.DialogInterface; import android.content.Intent; import android.database.Cursor; import android.net.Uri; -import android.os.Looper; import android.provider.Settings; import android.support.annotation.Nullable; +import android.support.v4.os.BuildCompat; import android.telecom.PhoneAccount; import android.telecom.PhoneAccountHandle; import android.telephony.PhoneNumberUtils; import android.telephony.TelephonyManager; import android.text.TextUtils; -import android.util.Log; import android.view.WindowManager; import android.widget.EditText; import android.widget.Toast; @@ -46,8 +45,11 @@ import com.android.contacts.common.database.NoNullCursorAsyncQueryHandler; import com.android.contacts.common.util.ContactDisplayUtils; import com.android.contacts.common.widget.SelectPhoneAccountDialogFragment; import com.android.contacts.common.widget.SelectPhoneAccountDialogFragment.SelectPhoneAccountListener; -import com.android.dialer.app.calllog.PhoneAccountUtils; +import com.android.dialer.calllogutils.PhoneAccountUtils; +import com.android.dialer.common.Assert; +import com.android.dialer.common.LogUtil; import com.android.dialer.compat.CompatUtils; +import com.android.dialer.oem.MotorolaUtils; import com.android.dialer.telecom.TelecomUtil; import java.util.ArrayList; import java.util.List; @@ -100,12 +102,19 @@ public class SpecialCharSequenceMgr { //get rid of the separators so that the string gets parsed correctly String dialString = PhoneNumberUtils.stripSeparators(input); - return handleDeviceIdDisplay(context, dialString) + if (handleDeviceIdDisplay(context, dialString) || handleRegulatoryInfoDisplay(context, dialString) || handlePinEntry(context, dialString) || handleAdnEntry(context, dialString, textField) - || handleSecretCode(context, dialString); + || handleSecretCode(context, dialString)) { + return true; + } + + if (MotorolaUtils.handleSpecialCharSequence(context, input)) { + return true; + } + return false; } /** @@ -114,10 +123,7 @@ public class SpecialCharSequenceMgr { *

This should be called when the screen becomes background. */ public static void cleanup() { - if (Looper.myLooper() != Looper.getMainLooper()) { - Log.wtf(TAG, "cleanup() is called outside the main thread"); - return; - } + Assert.isMainThread(); if (sPreviousAdnQueryHandler != null) { sPreviousAdnQueryHandler.cancel(); @@ -126,14 +132,21 @@ public class SpecialCharSequenceMgr { } /** - * Handles secret codes to launch arbitrary activities in the form of *#*##*#*. If a secret - * code is encountered an Intent is started with the android_secret_code:// URI. + * Handles secret codes to launch arbitrary activities in the form of *#*##*#*. + * If a secret code is encountered, an Intent is started with the android_secret_code:// + * URI. * * @param context the context to use * @param input the text to check for a secret code in - * @return true if a secret code was encountered + * @return true if a secret code was encountered and intent is sent out */ static boolean handleSecretCode(Context context, String input) { + // Must use system service on O+ to avoid using broadcasts, which are not allowed on O+. + if (BuildCompat.isAtLeastO()) { + return context.getSystemService(TelephonyManager.class).sendDialerCode(input); + } + + // System service call is not supported pre-O, so must use a broadcast for N-. // Secret codes are in the form *#*##*#* int len = input.length(); if (len > 8 && input.startsWith("*#*#") && input.endsWith("#*#*")) { @@ -144,7 +157,6 @@ public class SpecialCharSequenceMgr { context.sendBroadcast(intent); return true; } - return false; } @@ -237,7 +249,7 @@ public class SpecialCharSequenceMgr { private static void handleAdnQuery(QueryHandler handler, SimContactQueryCookie cookie, Uri uri) { if (handler == null || cookie == null || uri == null) { - Log.w(TAG, "queryAdn parameters incorrect"); + LogUtil.w("SpecialCharSequenceMgr.handleAdnQuery", "queryAdn parameters incorrect"); return; } @@ -325,12 +337,14 @@ public class SpecialCharSequenceMgr { private static boolean handleRegulatoryInfoDisplay(Context context, String input) { if (input.equals(MMI_REGULATORY_INFO_DISPLAY)) { - Log.d(TAG, "handleRegulatoryInfoDisplay() sending intent to settings app"); + LogUtil.i( + "SpecialCharSequenceMgr.handleRegulatoryInfoDisplay", "sending intent to settings app"); Intent showRegInfoIntent = new Intent(Settings.ACTION_SHOW_REGULATORY_INFO); try { context.startActivity(showRegInfoIntent); } catch (ActivityNotFoundException e) { - Log.e(TAG, "startActivity() failed: " + e); + LogUtil.e( + "SpecialCharSequenceMgr.handleRegulatoryInfoDisplay", "startActivity() failed: ", e); } return true; } diff --git a/java/com/android/dialer/app/calllog/CallDetailHistoryAdapter.java b/java/com/android/dialer/app/calllog/CallDetailHistoryAdapter.java deleted file mode 100644 index ab6ef7362..000000000 --- a/java/com/android/dialer/app/calllog/CallDetailHistoryAdapter.java +++ /dev/null @@ -1,214 +0,0 @@ -/* - * Copyright (C) 2011 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.app.calllog; - -import android.content.Context; -import android.icu.lang.UCharacter; -import android.icu.text.BreakIterator; -import android.os.Build.VERSION; -import android.os.Build.VERSION_CODES; -import android.provider.CallLog.Calls; -import android.text.format.DateUtils; -import android.text.format.Formatter; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.BaseAdapter; -import android.widget.TextView; -import com.android.dialer.app.PhoneCallDetails; -import com.android.dialer.app.R; -import com.android.dialer.util.CallUtil; -import com.android.dialer.util.DialerUtils; -import java.util.ArrayList; -import java.util.Locale; - -/** Adapter for a ListView containing history items from the details of a call. */ -public class CallDetailHistoryAdapter extends BaseAdapter { - - /** Each history item shows the detail of a call. */ - private static final int VIEW_TYPE_HISTORY_ITEM = 1; - - private final Context mContext; - private final LayoutInflater mLayoutInflater; - private final CallTypeHelper mCallTypeHelper; - private final PhoneCallDetails[] mPhoneCallDetails; - - /** List of items to be concatenated together for duration strings. */ - private ArrayList mDurationItems = new ArrayList<>(); - - public CallDetailHistoryAdapter( - Context context, - LayoutInflater layoutInflater, - CallTypeHelper callTypeHelper, - PhoneCallDetails[] phoneCallDetails) { - mContext = context; - mLayoutInflater = layoutInflater; - mCallTypeHelper = callTypeHelper; - mPhoneCallDetails = phoneCallDetails; - } - - @Override - public boolean isEnabled(int position) { - // None of history will be clickable. - return false; - } - - @Override - public int getCount() { - return mPhoneCallDetails.length; - } - - @Override - public Object getItem(int position) { - return mPhoneCallDetails[position]; - } - - @Override - public long getItemId(int position) { - return position; - } - - @Override - public int getViewTypeCount() { - return 1; - } - - @Override - public int getItemViewType(int position) { - return VIEW_TYPE_HISTORY_ITEM; - } - - @Override - public View getView(int position, View convertView, ViewGroup parent) { - // Make sure we have a valid convertView to start with - final View result = - convertView == null - ? mLayoutInflater.inflate(R.layout.call_detail_history_item, parent, false) - : convertView; - - PhoneCallDetails details = mPhoneCallDetails[position]; - CallTypeIconsView callTypeIconView = - (CallTypeIconsView) result.findViewById(R.id.call_type_icon); - TextView callTypeTextView = (TextView) result.findViewById(R.id.call_type_text); - TextView dateView = (TextView) result.findViewById(R.id.date); - TextView durationView = (TextView) result.findViewById(R.id.duration); - - int callType = details.callTypes[0]; - boolean isVideoCall = - (details.features & Calls.FEATURES_VIDEO) == Calls.FEATURES_VIDEO - && CallUtil.isVideoEnabled(mContext); - boolean isPulledCall = - (details.features & Calls.FEATURES_PULLED_EXTERNALLY) == Calls.FEATURES_PULLED_EXTERNALLY; - - callTypeIconView.clear(); - callTypeIconView.add(callType); - callTypeIconView.setShowVideo(isVideoCall); - callTypeTextView.setText(mCallTypeHelper.getCallTypeText(callType, isVideoCall, isPulledCall)); - // Set the date. - dateView.setText(formatDate(details.date)); - // Set the duration - if (Calls.VOICEMAIL_TYPE == callType || CallTypeHelper.isMissedCallType(callType)) { - durationView.setVisibility(View.GONE); - } else { - durationView.setVisibility(View.VISIBLE); - durationView.setText(formatDurationAndDataUsage(details.duration, details.dataUsage)); - } - - return result; - } - - /** - * Formats the provided date into a value suitable for display in the current locale. - * - *

For example, returns a string like "Wednesday, May 25, 2016, 8:02PM" or "Chorshanba, 2016 - * may 25,20:02". - * - *

For pre-N devices, the returned value may not start with a capital if the local convention - * is to not capitalize day names. On N+ devices, the returned value is always capitalized. - */ - private CharSequence formatDate(long callDateMillis) { - CharSequence dateValue = - DateUtils.formatDateRange( - mContext, - callDateMillis /* startDate */, - callDateMillis /* endDate */, - DateUtils.FORMAT_SHOW_TIME - | DateUtils.FORMAT_SHOW_DATE - | DateUtils.FORMAT_SHOW_WEEKDAY - | DateUtils.FORMAT_SHOW_YEAR); - - // We want the beginning of the date string to be capitalized, even if the word at the beginning - // of the string is not usually capitalized. For example, "Wednesdsay" in Uzbek is "chorshanba” - // (not capitalized). To handle this issue we apply title casing to the start of the sentence so - // that "chorshanba, 2016 may 25,20:02" becomes "Chorshanba, 2016 may 25,20:02". - // - // The ICU library was not available in Android until N, so we can only do this in N+ devices. - // Pre-N devices will still see incorrect capitalization in some languages. - if (VERSION.SDK_INT < VERSION_CODES.N) { - return dateValue; - } - - // Using the ICU library is safer than just applying toUpperCase() on the first letter of the - // word because in some languages, there can be multiple starting characters which should be - // upper-cased together. For example in Dutch "ij" is a digraph in which both letters should be - // capitalized together. - - // TITLECASE_NO_LOWERCASE is necessary so that things that are already capitalized like the - // month ("May") are not lower-cased as part of the conversion. - return UCharacter.toTitleCase( - Locale.getDefault(), - dateValue.toString(), - BreakIterator.getSentenceInstance(), - UCharacter.TITLECASE_NO_LOWERCASE); - } - - private CharSequence formatDuration(long elapsedSeconds) { - long minutes = 0; - long seconds = 0; - - if (elapsedSeconds >= 60) { - minutes = elapsedSeconds / 60; - elapsedSeconds -= minutes * 60; - seconds = elapsedSeconds; - return mContext.getString(R.string.callDetailsDurationFormat, minutes, seconds); - } else { - seconds = elapsedSeconds; - return mContext.getString(R.string.callDetailsShortDurationFormat, seconds); - } - } - - /** - * Formats a string containing the call duration and the data usage (if specified). - * - * @param elapsedSeconds Total elapsed seconds. - * @param dataUsage Data usage in bytes, or null if not specified. - * @return String containing call duration and data usage. - */ - private CharSequence formatDurationAndDataUsage(long elapsedSeconds, Long dataUsage) { - CharSequence duration = formatDuration(elapsedSeconds); - - if (dataUsage != null) { - mDurationItems.clear(); - mDurationItems.add(duration); - mDurationItems.add(Formatter.formatShortFileSize(mContext, dataUsage)); - - return DialerUtils.join(mDurationItems); - } else { - return duration; - } - } -} diff --git a/java/com/android/dialer/app/calllog/CallLogActivity.java b/java/com/android/dialer/app/calllog/CallLogActivity.java new file mode 100644 index 000000000..719ab4369 --- /dev/null +++ b/java/com/android/dialer/app/calllog/CallLogActivity.java @@ -0,0 +1,220 @@ +/* + * Copyright (C) 2013 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.app.calllog; + +import android.app.Fragment; +import android.app.FragmentManager; +import android.content.Intent; +import android.os.Bundle; +import android.provider.CallLog; +import android.provider.CallLog.Calls; +import android.support.v13.app.FragmentPagerAdapter; +import android.support.v4.view.ViewPager; +import android.support.v7.app.ActionBar; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; +import android.view.ViewGroup; +import com.android.contacts.common.list.ViewPagerTabs; +import com.android.dialer.app.DialtactsActivity; +import com.android.dialer.app.R; +import com.android.dialer.database.CallLogQueryHandler; +import com.android.dialer.logging.Logger; +import com.android.dialer.logging.nano.ScreenEvent; +import com.android.dialer.util.TransactionSafeActivity; +import com.android.dialer.util.ViewUtil; + +/** Activity for viewing call history. */ +public class CallLogActivity extends TransactionSafeActivity + implements ViewPager.OnPageChangeListener { + + private static final int TAB_INDEX_ALL = 0; + private static final int TAB_INDEX_MISSED = 1; + private static final int TAB_INDEX_COUNT = 2; + private ViewPager mViewPager; + private ViewPagerTabs mViewPagerTabs; + private ViewPagerAdapter mViewPagerAdapter; + private CallLogFragment mAllCallsFragment; + private CallLogFragment mMissedCallsFragment; + private String[] mTabTitles; + private boolean mIsResumed; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + setContentView(R.layout.call_log_activity); + getWindow().setBackgroundDrawable(null); + + final ActionBar actionBar = getSupportActionBar(); + actionBar.setDisplayShowHomeEnabled(true); + actionBar.setDisplayHomeAsUpEnabled(true); + actionBar.setDisplayShowTitleEnabled(true); + actionBar.setElevation(0); + + int startingTab = TAB_INDEX_ALL; + final Intent intent = getIntent(); + if (intent != null) { + final int callType = intent.getIntExtra(CallLog.Calls.EXTRA_CALL_TYPE_FILTER, -1); + if (callType == CallLog.Calls.MISSED_TYPE) { + startingTab = TAB_INDEX_MISSED; + } + } + + mTabTitles = new String[TAB_INDEX_COUNT]; + mTabTitles[0] = getString(R.string.call_log_all_title); + mTabTitles[1] = getString(R.string.call_log_missed_title); + + mViewPager = (ViewPager) findViewById(R.id.call_log_pager); + + mViewPagerAdapter = new ViewPagerAdapter(getFragmentManager()); + mViewPager.setAdapter(mViewPagerAdapter); + mViewPager.setOffscreenPageLimit(1); + mViewPager.setOnPageChangeListener(this); + + mViewPagerTabs = (ViewPagerTabs) findViewById(R.id.viewpager_header); + + mViewPagerTabs.setViewPager(mViewPager); + mViewPager.setCurrentItem(startingTab); + } + + @Override + protected void onResume() { + mIsResumed = true; + super.onResume(); + sendScreenViewForChildFragment(mViewPager.getCurrentItem()); + } + + @Override + protected void onPause() { + mIsResumed = false; + super.onPause(); + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + final MenuInflater inflater = getMenuInflater(); + inflater.inflate(R.menu.call_log_options, menu); + return true; + } + + @Override + public boolean onPrepareOptionsMenu(Menu menu) { + final MenuItem itemDeleteAll = menu.findItem(R.id.delete_all); + if (mAllCallsFragment != null && itemDeleteAll != null) { + // If onPrepareOptionsMenu is called before fragments are loaded, don't do anything. + final CallLogAdapter adapter = mAllCallsFragment.getAdapter(); + itemDeleteAll.setVisible(adapter != null && !adapter.isEmpty()); + } + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + if (!isSafeToCommitTransactions()) { + return true; + } + + if (item.getItemId() == android.R.id.home) { + final Intent intent = new Intent(this, DialtactsActivity.class); + intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); + startActivity(intent); + return true; + } else if (item.getItemId() == R.id.delete_all) { + ClearCallLogDialog.show(getFragmentManager()); + return true; + } + return super.onOptionsItemSelected(item); + } + + @Override + public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { + mViewPagerTabs.onPageScrolled(position, positionOffset, positionOffsetPixels); + } + + @Override + public void onPageSelected(int position) { + if (mIsResumed) { + sendScreenViewForChildFragment(position); + } + mViewPagerTabs.onPageSelected(position); + } + + @Override + public void onPageScrollStateChanged(int state) { + mViewPagerTabs.onPageScrollStateChanged(state); + } + + private void sendScreenViewForChildFragment(int position) { + Logger.get(this).logScreenView(ScreenEvent.Type.CALL_LOG_FILTER, this); + } + + private int getRtlPosition(int position) { + if (ViewUtil.isRtl()) { + return mViewPagerAdapter.getCount() - 1 - position; + } + return position; + } + + /** Adapter for the view pager. */ + public class ViewPagerAdapter extends FragmentPagerAdapter { + + public ViewPagerAdapter(FragmentManager fm) { + super(fm); + } + + @Override + public long getItemId(int position) { + return getRtlPosition(position); + } + + @Override + public Fragment getItem(int position) { + switch (getRtlPosition(position)) { + case TAB_INDEX_ALL: + return new CallLogFragment( + CallLogQueryHandler.CALL_TYPE_ALL, true /* isCallLogActivity */); + case TAB_INDEX_MISSED: + return new CallLogFragment(Calls.MISSED_TYPE, true /* isCallLogActivity */); + } + throw new IllegalStateException("No fragment at position " + position); + } + + @Override + public Object instantiateItem(ViewGroup container, int position) { + final CallLogFragment fragment = (CallLogFragment) super.instantiateItem(container, position); + switch (position) { + case TAB_INDEX_ALL: + mAllCallsFragment = fragment; + break; + case TAB_INDEX_MISSED: + mMissedCallsFragment = fragment; + break; + } + return fragment; + } + + @Override + public CharSequence getPageTitle(int position) { + return mTabTitles[position]; + } + + @Override + public int getCount() { + return TAB_INDEX_COUNT; + } + } +} diff --git a/java/com/android/dialer/app/calllog/CallLogAdapter.java b/java/com/android/dialer/app/calllog/CallLogAdapter.java index ea09a8c0a..fc5ffbb29 100644 --- a/java/com/android/dialer/app/calllog/CallLogAdapter.java +++ b/java/com/android/dialer/app/calllog/CallLogAdapter.java @@ -47,7 +47,6 @@ import com.android.contacts.common.compat.PhoneNumberUtilsCompat; import com.android.contacts.common.preference.ContactsPreferences; import com.android.dialer.app.Bindings; import com.android.dialer.app.DialtactsActivity; -import com.android.dialer.app.PhoneCallDetails; import com.android.dialer.app.R; import com.android.dialer.app.calllog.CallLogGroupBuilder.GroupCreator; import com.android.dialer.app.calllog.calllogcache.CallLogCache; @@ -55,13 +54,19 @@ import com.android.dialer.app.contactinfo.ContactInfoCache; import com.android.dialer.app.voicemail.VoicemailPlaybackPresenter; import com.android.dialer.app.voicemail.VoicemailPlaybackPresenter.OnVoicemailDeletedListener; import com.android.dialer.blocking.FilteredNumberAsyncQueryHandler; +import com.android.dialer.calldetails.nano.CallDetailsEntries; +import com.android.dialer.calldetails.nano.CallDetailsEntries.CallDetailsEntry; +import com.android.dialer.calllogutils.PhoneAccountUtils; +import com.android.dialer.calllogutils.PhoneCallDetails; import com.android.dialer.common.Assert; import com.android.dialer.common.AsyncTaskExecutor; import com.android.dialer.common.AsyncTaskExecutors; import com.android.dialer.common.LogUtil; import com.android.dialer.enrichedcall.EnrichedCallCapabilities; +import com.android.dialer.enrichedcall.EnrichedCallComponent; import com.android.dialer.enrichedcall.EnrichedCallManager; import com.android.dialer.enrichedcall.EnrichedCallManager.CapabilitiesListener; +import com.android.dialer.enrichedcall.historyquery.proto.nano.HistoryResult; import com.android.dialer.logging.Logger; import com.android.dialer.logging.nano.DialerImpression; import com.android.dialer.phonenumbercache.CallLogQuery; @@ -70,6 +75,7 @@ import com.android.dialer.phonenumbercache.ContactInfoHelper; import com.android.dialer.phonenumberutil.PhoneNumberHelper; import com.android.dialer.spam.Spam; import com.android.dialer.util.PermissionsUtil; +import java.util.List; import java.util.Map; import java.util.Set; @@ -96,7 +102,7 @@ public class CallLogAdapter extends GroupingListAdapter protected final CallLogCache mCallLogCache; private final CallFetcher mCallFetcher; - private final FilteredNumberAsyncQueryHandler mFilteredNumberAsyncQueryHandler; + @NonNull private final FilteredNumberAsyncQueryHandler mFilteredNumberAsyncQueryHandler; private final int mActivityType; /** Instance of helper class for managing views. */ @@ -182,8 +188,6 @@ public class CallLogAdapter extends GroupingListAdapter private boolean mIsSpamEnabled; - @NonNull private final EnrichedCallManager mEnrichedCallManager; - public CallLogAdapter( Activity activity, ViewGroup alertContainer, @@ -191,6 +195,7 @@ public class CallLogAdapter extends GroupingListAdapter CallLogCache callLogCache, ContactInfoCache contactInfoCache, VoicemailPlaybackPresenter voicemailPlaybackPresenter, + @NonNull FilteredNumberAsyncQueryHandler filteredNumberAsyncQueryHandler, int activityType) { super(); @@ -218,7 +223,7 @@ public class CallLogAdapter extends GroupingListAdapter mCallLogListItemHelper = new CallLogListItemHelper(phoneCallDetailsHelper, resources, mCallLogCache); mCallLogGroupBuilder = new CallLogGroupBuilder(this); - mFilteredNumberAsyncQueryHandler = new FilteredNumberAsyncQueryHandler(mActivity); + mFilteredNumberAsyncQueryHandler = Assert.isNotNull(filteredNumberAsyncQueryHandler); mContactsPreferences = new ContactsPreferences(mActivity); @@ -232,7 +237,6 @@ public class CallLogAdapter extends GroupingListAdapter mCallLogAlertManager = new CallLogAlertManager(this, LayoutInflater.from(mActivity), alertContainer); - mEnrichedCallManager = EnrichedCallManager.Accessor.getInstance(activity.getApplication()); } private void expandViewHolderActions(CallLogListItemViewHolder viewHolder) { @@ -296,7 +300,7 @@ public class CallLogAdapter extends GroupingListAdapter } mContactsPreferences.refreshValue(ContactsPreferences.DISPLAY_ORDER_KEY); mIsSpamEnabled = Spam.get(mActivity).isSpamEnabled(); - mEnrichedCallManager.registerCapabilitiesListener(this); + getEnrichedCallManager().registerCapabilitiesListener(this); notifyDataSetChanged(); } @@ -305,11 +309,11 @@ public class CallLogAdapter extends GroupingListAdapter for (Uri uri : mHiddenItemUris) { CallLogAsyncTaskUtil.deleteVoicemail(mActivity, uri, null); } - mEnrichedCallManager.unregisterCapabilitiesListener(this); + getEnrichedCallManager().unregisterCapabilitiesListener(this); } public void onStop() { - mEnrichedCallManager.clearCachedData(); + getEnrichedCallManager().clearCachedData(); } public CallLogAlertManager getAlertManager() { @@ -420,7 +424,9 @@ public class CallLogAdapter extends GroupingListAdapter } CallLogListItemViewHolder views = (CallLogListItemViewHolder) viewHolder; views.isLoaded = false; - PhoneCallDetails details = createPhoneCallDetails(c, getGroupSize(position), views); + int groupSize = getGroupSize(position); + CallDetailsEntries callDetailsEntries = createCallDetailsEntries(c, groupSize); + PhoneCallDetails details = createPhoneCallDetails(c, groupSize, views); if (mHiddenRowIds.contains(c.getLong(CallLogQuery.ID))) { views.callLogEntryView.setVisibility(View.GONE); views.dayGroupHeader.setVisibility(View.GONE); @@ -432,11 +438,14 @@ public class CallLogAdapter extends GroupingListAdapter if (mCurrentlyExpandedRowId == views.rowId) { views.inflateActionViewStub(); } - loadAndRender(views, views.rowId, details); + loadAndRender(views, views.rowId, details, callDetailsEntries); } private void loadAndRender( - final CallLogListItemViewHolder views, final long rowId, final PhoneCallDetails details) { + final CallLogListItemViewHolder views, + final long rowId, + final PhoneCallDetails details, + final CallDetailsEntries callDetailsEntries) { // Reset block and spam information since this view could be reused which may contain // outdated data. views.isSpam = false; @@ -464,12 +473,33 @@ public class CallLogAdapter extends GroupingListAdapter && Spam.get(mActivity) .checkSpamStatusSynchronous(views.number, views.countryIso); details.isSpam = views.isSpam; - if (isCancelled()) { - return false; + } + if (isCancelled()) { + return false; + } + setCallDetailsEntriesHistoryResults( + PhoneNumberUtils.formatNumberToE164(views.number, views.countryIso), + callDetailsEntries); + views.setDetailedPhoneDetails(callDetailsEntries); + return !isCancelled() && loadData(views, rowId, details); + } + + private void setCallDetailsEntriesHistoryResults( + @Nullable String number, CallDetailsEntries callDetailsEntries) { + if (number == null) { + return; + } + Map> mappedResults = + getEnrichedCallManager().getAllHistoricalData(number, callDetailsEntries); + for (CallDetailsEntry entry : callDetailsEntries.entries) { + List results = mappedResults.get(entry); + if (results != null) { + entry.historyResults = mappedResults.get(entry).toArray(new HistoryResult[0]); + LogUtil.v( + "CallLogAdapter.setCallDetailsEntriesHistoryResults", + "mapped %d results", + entry.historyResults.length); } - return loadData(views, rowId, details); - } else { - return loadData(views, rowId, details); } } @@ -499,9 +529,9 @@ public class CallLogAdapter extends GroupingListAdapter return false; } - EnrichedCallCapabilities capabilities = mEnrichedCallManager.getCapabilities(e164Number); + EnrichedCallCapabilities capabilities = getEnrichedCallManager().getCapabilities(e164Number); if (capabilities == null) { - mEnrichedCallManager.requestCapabilities(e164Number); + getEnrichedCallManager().requestCapabilities(e164Number); return false; } return capabilities.supportsCallComposer(); @@ -562,6 +592,27 @@ public class CallLogAdapter extends GroupingListAdapter return details; } + @MainThread + private static CallDetailsEntries createCallDetailsEntries(Cursor cursor, int count) { + Assert.isMainThread(); + int position = cursor.getPosition(); + CallDetailsEntries entries = new CallDetailsEntries(); + entries.entries = new CallDetailsEntry[count]; + for (int i = 0; i < count; i++) { + CallDetailsEntry entry = new CallDetailsEntry(); + entry.callId = cursor.getLong(CallLogQuery.ID); + entry.callType = cursor.getInt(CallLogQuery.CALL_TYPE); + entry.dataUsage = cursor.getLong(CallLogQuery.DATA_USAGE); + entry.date = cursor.getLong(CallLogQuery.DATE); + entry.duration = cursor.getLong(CallLogQuery.DURATION); + entry.features |= cursor.getInt(CallLogQuery.FEATURES); + entries.entries[i] = entry; + cursor.moveToNext(); + } + cursor.moveToPosition(position); + return entries; + } + /** * Load data for call log. Any expensive operation should be put here to avoid blocking main * thread. Do NOT put any cursor operation here since it's not thread safe. @@ -907,6 +958,11 @@ public class CallLogAdapter extends GroupingListAdapter notifyDataSetChanged(); } + @NonNull + private EnrichedCallManager getEnrichedCallManager() { + return EnrichedCallComponent.get(mActivity).getEnrichedCallManager(); + } + /** Interface used to initiate a refresh of the content. */ public interface CallFetcher { diff --git a/java/com/android/dialer/app/calllog/CallLogAsyncTaskUtil.java b/java/com/android/dialer/app/calllog/CallLogAsyncTaskUtil.java index b4e6fc5ad..2198626d6 100644 --- a/java/com/android/dialer/app/calllog/CallLogAsyncTaskUtil.java +++ b/java/com/android/dialer/app/calllog/CallLogAsyncTaskUtil.java @@ -16,37 +16,22 @@ package com.android.dialer.app.calllog; -import android.Manifest.permission; import android.annotation.TargetApi; import android.content.ContentValues; import android.content.Context; import android.content.Intent; -import android.content.pm.PackageManager; -import android.database.Cursor; import android.net.Uri; import android.os.AsyncTask; -import android.os.Build.VERSION; import android.os.Build.VERSION_CODES; import android.provider.CallLog; import android.provider.VoicemailContract.Voicemails; import android.support.annotation.NonNull; import android.support.annotation.Nullable; -import android.support.annotation.VisibleForTesting; -import android.support.v4.content.ContextCompat; -import android.telecom.PhoneAccountHandle; import android.text.TextUtils; -import com.android.contacts.common.GeoUtil; -import com.android.dialer.app.PhoneCallDetails; import com.android.dialer.common.AsyncTaskExecutor; import com.android.dialer.common.AsyncTaskExecutors; -import com.android.dialer.common.LogUtil; -import com.android.dialer.phonenumbercache.ContactInfo; -import com.android.dialer.phonenumbercache.ContactInfoHelper; -import com.android.dialer.phonenumberutil.PhoneNumberHelper; -import com.android.dialer.telecom.TelecomUtil; import com.android.dialer.util.PermissionsUtil; -import java.util.ArrayList; -import java.util.Arrays; +import com.android.voicemail.VoicemailClient; @TargetApi(VERSION_CODES.M) public class CallLogAsyncTaskUtil { @@ -58,166 +43,6 @@ public class CallLogAsyncTaskUtil { sAsyncTaskExecutor = AsyncTaskExecutors.createThreadPoolExecutor(); } - public static void getCallDetails( - @NonNull final Context context, - @Nullable final CallLogAsyncTaskListener callLogAsyncTaskListener, - @NonNull final Uri... callUris) { - if (sAsyncTaskExecutor == null) { - initTaskExecutor(); - } - - sAsyncTaskExecutor.submit( - Tasks.GET_CALL_DETAILS, - new AsyncTask() { - @Override - public PhoneCallDetails[] doInBackground(Void... params) { - if (ContextCompat.checkSelfPermission(context, permission.READ_CALL_LOG) - != PackageManager.PERMISSION_GRANTED) { - LogUtil.w("CallLogAsyncTaskUtil.getCallDetails", "missing READ_CALL_LOG permission"); - return null; - } - // TODO: All calls correspond to the same person, so make a single lookup. - final int numCalls = callUris.length; - PhoneCallDetails[] details = new PhoneCallDetails[numCalls]; - try { - for (int index = 0; index < numCalls; ++index) { - details[index] = getPhoneCallDetailsForUri(context, callUris[index]); - } - return details; - } catch (IllegalArgumentException e) { - // Something went wrong reading in our primary data. - LogUtil.e( - "CallLogAsyncTaskUtil.getCallDetails", "invalid URI starting call details", e); - return null; - } - } - - @Override - public void onPostExecute(PhoneCallDetails[] phoneCallDetails) { - if (callLogAsyncTaskListener != null) { - callLogAsyncTaskListener.onGetCallDetails(phoneCallDetails); - } - } - }); - } - - /** Return the phone call details for a given call log URI. */ - private static PhoneCallDetails getPhoneCallDetailsForUri( - @NonNull Context context, @NonNull Uri callUri) { - Cursor cursor = - context - .getContentResolver() - .query(callUri, CallDetailQuery.CALL_LOG_PROJECTION, null, null, null); - - try { - if (cursor == null || !cursor.moveToFirst()) { - throw new IllegalArgumentException("Cannot find content: " + callUri); - } - - // Read call log. - final String countryIso = cursor.getString(CallDetailQuery.COUNTRY_ISO_COLUMN_INDEX); - final String number = cursor.getString(CallDetailQuery.NUMBER_COLUMN_INDEX); - final String postDialDigits = - (VERSION.SDK_INT >= VERSION_CODES.N) - ? cursor.getString(CallDetailQuery.POST_DIAL_DIGITS) - : ""; - final String viaNumber = - (VERSION.SDK_INT >= VERSION_CODES.N) ? cursor.getString(CallDetailQuery.VIA_NUMBER) : ""; - final int numberPresentation = - cursor.getInt(CallDetailQuery.NUMBER_PRESENTATION_COLUMN_INDEX); - - final PhoneAccountHandle accountHandle = - PhoneAccountUtils.getAccount( - cursor.getString(CallDetailQuery.ACCOUNT_COMPONENT_NAME), - cursor.getString(CallDetailQuery.ACCOUNT_ID)); - - // If this is not a regular number, there is no point in looking it up in the contacts. - ContactInfoHelper contactInfoHelper = - new ContactInfoHelper(context, GeoUtil.getCurrentCountryIso(context)); - boolean isVoicemail = PhoneNumberHelper.isVoicemailNumber(context, accountHandle, number); - boolean shouldLookupNumber = - PhoneNumberHelper.canPlaceCallsTo(number, numberPresentation) && !isVoicemail; - ContactInfo info = ContactInfo.EMPTY; - - if (shouldLookupNumber) { - ContactInfo lookupInfo = contactInfoHelper.lookupNumber(number, countryIso); - info = lookupInfo != null ? lookupInfo : ContactInfo.EMPTY; - } - - PhoneCallDetails details = new PhoneCallDetails(number, numberPresentation, postDialDigits); - details.updateDisplayNumber(context, info.formattedNumber, isVoicemail); - - details.viaNumber = viaNumber; - details.accountHandle = accountHandle; - details.contactUri = info.lookupUri; - details.namePrimary = info.name; - details.nameAlternative = info.nameAlternative; - details.numberType = info.type; - details.numberLabel = info.label; - details.photoUri = info.photoUri; - details.sourceType = info.sourceType; - details.objectId = info.objectId; - - details.callTypes = new int[] {cursor.getInt(CallDetailQuery.CALL_TYPE_COLUMN_INDEX)}; - details.date = cursor.getLong(CallDetailQuery.DATE_COLUMN_INDEX); - details.duration = cursor.getLong(CallDetailQuery.DURATION_COLUMN_INDEX); - details.features = cursor.getInt(CallDetailQuery.FEATURES); - details.geocode = cursor.getString(CallDetailQuery.GEOCODED_LOCATION_COLUMN_INDEX); - details.transcription = cursor.getString(CallDetailQuery.TRANSCRIPTION_COLUMN_INDEX); - - details.countryIso = - !TextUtils.isEmpty(countryIso) ? countryIso : GeoUtil.getCurrentCountryIso(context); - - if (!cursor.isNull(CallDetailQuery.DATA_USAGE)) { - details.dataUsage = cursor.getLong(CallDetailQuery.DATA_USAGE); - } - - return details; - } finally { - if (cursor != null) { - cursor.close(); - } - } - } - - /** - * Delete specified calls from the call log. - * - * @param context The context. - * @param callIds String of the callIds to delete from the call log, delimited by commas (","). - * @param callLogAsyncTaskListener The listener to invoke after the entries have been deleted. - */ - public static void deleteCalls( - @NonNull final Context context, - final String callIds, - @Nullable final CallLogAsyncTaskListener callLogAsyncTaskListener) { - if (sAsyncTaskExecutor == null) { - initTaskExecutor(); - } - - sAsyncTaskExecutor.submit( - Tasks.DELETE_CALL, - new AsyncTask() { - @Override - public Void doInBackground(Void... params) { - context - .getContentResolver() - .delete( - TelecomUtil.getCallLogUri(context), - CallLog.Calls._ID + " IN (" + callIds + ")", - null); - return null; - } - - @Override - public void onPostExecute(Void result) { - if (callLogAsyncTaskListener != null) { - callLogAsyncTaskListener.onDeleteCall(); - } - } - }); - } - public static void markVoicemailAsRead( @NonNull final Context context, @NonNull final Uri voicemailUri) { if (sAsyncTaskExecutor == null) { @@ -235,6 +60,8 @@ public class CallLogAsyncTaskUtil { .getContentResolver() .update(voicemailUri, values, Voicemails.IS_READ + " = 0", null); + uploadVoicemailLocalChangesToServer(context); + Intent intent = new Intent(context, CallLogNotificationsService.class); intent.setAction(CallLogNotificationsService.ACTION_MARK_NEW_VOICEMAILS_AS_OLD); context.startService(intent); @@ -256,7 +83,12 @@ public class CallLogAsyncTaskUtil { new AsyncTask() { @Override public Void doInBackground(Void... params) { - context.getContentResolver().delete(voicemailUri, null, null); + ContentValues values = new ContentValues(); + values.put(Voicemails.DELETED, "1"); + context.getContentResolver().update(voicemailUri, values, null, null); + // TODO(b/35440541): check which source package is changed. Don't need + // to upload changes on foreign voicemails, they will get a PROVIDER_CHANGED + uploadVoicemailLocalChangesToServer(context); return null; } @@ -305,11 +137,6 @@ public class CallLogAsyncTaskUtil { }); } - @VisibleForTesting - public static void resetForTest() { - sAsyncTaskExecutor = null; - } - /** The enumeration of {@link AsyncTask} objects used in this class. */ public enum Tasks { DELETE_VOICEMAIL, @@ -321,56 +148,12 @@ public class CallLogAsyncTaskUtil { } public interface CallLogAsyncTaskListener { - - void onDeleteCall(); - void onDeleteVoicemail(); - - void onGetCallDetails(PhoneCallDetails[] details); } - private static final class CallDetailQuery { - - public static final String[] CALL_LOG_PROJECTION; - static final int DATE_COLUMN_INDEX = 0; - static final int DURATION_COLUMN_INDEX = 1; - static final int NUMBER_COLUMN_INDEX = 2; - static final int CALL_TYPE_COLUMN_INDEX = 3; - static final int COUNTRY_ISO_COLUMN_INDEX = 4; - static final int GEOCODED_LOCATION_COLUMN_INDEX = 5; - static final int NUMBER_PRESENTATION_COLUMN_INDEX = 6; - static final int ACCOUNT_COMPONENT_NAME = 7; - static final int ACCOUNT_ID = 8; - static final int FEATURES = 9; - static final int DATA_USAGE = 10; - static final int TRANSCRIPTION_COLUMN_INDEX = 11; - static final int POST_DIAL_DIGITS = 12; - static final int VIA_NUMBER = 13; - private static final String[] CALL_LOG_PROJECTION_INTERNAL = - new String[] { - CallLog.Calls.DATE, - CallLog.Calls.DURATION, - CallLog.Calls.NUMBER, - CallLog.Calls.TYPE, - CallLog.Calls.COUNTRY_ISO, - CallLog.Calls.GEOCODED_LOCATION, - CallLog.Calls.NUMBER_PRESENTATION, - CallLog.Calls.PHONE_ACCOUNT_COMPONENT_NAME, - CallLog.Calls.PHONE_ACCOUNT_ID, - CallLog.Calls.FEATURES, - CallLog.Calls.DATA_USAGE, - CallLog.Calls.TRANSCRIPTION - }; - - static { - ArrayList projectionList = new ArrayList<>(); - projectionList.addAll(Arrays.asList(CALL_LOG_PROJECTION_INTERNAL)); - if (VERSION.SDK_INT >= VERSION_CODES.N) { - projectionList.add(CallLog.Calls.POST_DIAL_DIGITS); - projectionList.add(CallLog.Calls.VIA_NUMBER); - } - projectionList.trimToSize(); - CALL_LOG_PROJECTION = projectionList.toArray(new String[projectionList.size()]); - } + private static void uploadVoicemailLocalChangesToServer(Context context) { + Intent intent = new Intent(VoicemailClient.ACTION_UPLOAD); + intent.setPackage(context.getPackageName()); + context.sendBroadcast(intent); } } diff --git a/java/com/android/dialer/app/calllog/CallLogFragment.java b/java/com/android/dialer/app/calllog/CallLogFragment.java index 1ae68cd65..4abef3430 100644 --- a/java/com/android/dialer/app/calllog/CallLogFragment.java +++ b/java/com/android/dialer/app/calllog/CallLogFragment.java @@ -53,6 +53,7 @@ import com.android.dialer.app.list.ListsFragment.ListsPage; import com.android.dialer.app.voicemail.VoicemailPlaybackPresenter; import com.android.dialer.app.widget.EmptyContentView; import com.android.dialer.app.widget.EmptyContentView.OnEmptyViewActionButtonClickedListener; +import com.android.dialer.blocking.FilteredNumberAsyncQueryHandler; import com.android.dialer.common.LogUtil; import com.android.dialer.database.CallLogQueryHandler; import com.android.dialer.phonenumbercache.ContactInfoHelper; @@ -70,9 +71,17 @@ public class CallLogFragment extends Fragment FragmentCompat.OnRequestPermissionsResultCallback, CallLogModalAlertManager.Listener { private static final String KEY_FILTER_TYPE = "filter_type"; + private static final String KEY_LOG_LIMIT = "log_limit"; + private static final String KEY_DATE_LIMIT = "date_limit"; + private static final String KEY_IS_CALL_LOG_ACTIVITY = "is_call_log_activity"; private static final String KEY_HAS_READ_CALL_LOG_PERMISSION = "has_read_call_log_permission"; private static final String KEY_REFRESH_DATA_REQUIRED = "refresh_data_required"; + // No limit specified for the number of logs to show; use the CallLogQueryHandler's default. + private static final int NO_LOG_LIMIT = -1; + // No date-based filtering. + private static final int NO_DATE_LIMIT = 0; + private static final int READ_CALL_LOG_PERMISSION_REQUEST_CODE = 1; private static final int EVENT_UPDATE_DISPLAY = 1; @@ -104,8 +113,17 @@ public class CallLogFragment extends Fragment // Exactly same variable is in Fragment as a package private. private boolean mMenuVisible = true; // Default to all calls. - protected int mCallTypeFilter = CallLogQueryHandler.CALL_TYPE_ALL; - + private int mCallTypeFilter = CallLogQueryHandler.CALL_TYPE_ALL; + // Log limit - if no limit is specified, then the default in {@link CallLogQueryHandler} + // will be used. + private int mLogLimit = NO_LOG_LIMIT; + // Date limit (in millis since epoch) - when non-zero, only calls which occurred on or after + // the date filter are included. If zero, no date-based filtering occurs. + private long mDateLimit = NO_DATE_LIMIT; + /* + * True if this instance of the CallLogFragment shown in the CallLogActivity. + */ + private boolean mIsCallLogActivity = false; private final Handler mDisplayUpdateHandler = new Handler() { @Override @@ -121,6 +139,48 @@ public class CallLogFragment extends Fragment protected CallLogModalAlertManager mModalAlertManager; private ViewGroup mModalAlertView; + public CallLogFragment() { + this(CallLogQueryHandler.CALL_TYPE_ALL, NO_LOG_LIMIT); + } + + public CallLogFragment(int filterType) { + this(filterType, NO_LOG_LIMIT); + } + + public CallLogFragment(int filterType, boolean isCallLogActivity) { + this(filterType, NO_LOG_LIMIT); + mIsCallLogActivity = isCallLogActivity; + } + + public CallLogFragment(int filterType, int logLimit) { + this(filterType, logLimit, NO_DATE_LIMIT); + } + + /** + * Creates a call log fragment, filtering to include only calls of the desired type, occurring + * after the specified date. + * + * @param filterType type of calls to include. + * @param dateLimit limits results to calls occurring on or after the specified date. + */ + public CallLogFragment(int filterType, long dateLimit) { + this(filterType, NO_LOG_LIMIT, dateLimit); + } + + /** + * Creates a call log fragment, filtering to include only calls of the desired type, occurring + * after the specified date. Also provides a means to limit the number of results returned. + * + * @param filterType type of calls to include. + * @param logLimit limits the number of results to return. + * @param dateLimit limits results to calls occurring on or after the specified date. + */ + public CallLogFragment(int filterType, int logLimit, long dateLimit) { + mCallTypeFilter = filterType; + mLogLimit = logLimit; + mDateLimit = dateLimit; + } + @Override public void onCreate(Bundle state) { LogUtil.d("CallLogFragment.onCreate", toString()); @@ -128,13 +188,16 @@ public class CallLogFragment extends Fragment mRefreshDataRequired = true; if (state != null) { mCallTypeFilter = state.getInt(KEY_FILTER_TYPE, mCallTypeFilter); + mLogLimit = state.getInt(KEY_LOG_LIMIT, mLogLimit); + mDateLimit = state.getLong(KEY_DATE_LIMIT, mDateLimit); + mIsCallLogActivity = state.getBoolean(KEY_IS_CALL_LOG_ACTIVITY, mIsCallLogActivity); mHasReadCallLogPermission = state.getBoolean(KEY_HAS_READ_CALL_LOG_PERMISSION, false); mRefreshDataRequired = state.getBoolean(KEY_REFRESH_DATA_REQUIRED, mRefreshDataRequired); } final Activity activity = getActivity(); final ContentResolver resolver = activity.getContentResolver(); - mCallLogQueryHandler = new CallLogQueryHandler(activity, resolver, this); + mCallLogQueryHandler = new CallLogQueryHandler(activity, resolver, this, mLogLimit); mKeyguardManager = (KeyguardManager) activity.getSystemService(Context.KEYGUARD_SERVICE); resolver.registerContentObserver(CallLog.CONTENT_URI, true, mCallLogObserver); resolver.registerContentObserver( @@ -226,7 +289,10 @@ public class CallLogFragment extends Fragment } protected void setupData() { - int activityType = CallLogAdapter.ACTIVITY_TYPE_DIALTACTS; + int activityType = + mIsCallLogActivity + ? CallLogAdapter.ACTIVITY_TYPE_CALL_LOG + : CallLogAdapter.ACTIVITY_TYPE_DIALTACTS; String currentCountryIso = GeoUtil.getCurrentCountryIso(getActivity()); mContactInfoCache = @@ -244,6 +310,7 @@ public class CallLogFragment extends Fragment CallLogCache.getCallLogCache(getActivity()), mContactInfoCache, getVoicemailPlaybackPresenter(), + new FilteredNumberAsyncQueryHandler(getActivity()), activityType); mRecyclerView.setAdapter(mAdapter); fetchCalls(); @@ -324,6 +391,9 @@ public class CallLogFragment extends Fragment public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putInt(KEY_FILTER_TYPE, mCallTypeFilter); + outState.putInt(KEY_LOG_LIMIT, mLogLimit); + outState.putLong(KEY_DATE_LIMIT, mDateLimit); + outState.putBoolean(KEY_IS_CALL_LOG_ACTIVITY, mIsCallLogActivity); outState.putBoolean(KEY_HAS_READ_CALL_LOG_PERMISSION, mHasReadCallLogPermission); outState.putBoolean(KEY_REFRESH_DATA_REQUIRED, mRefreshDataRequired); @@ -334,8 +404,10 @@ public class CallLogFragment extends Fragment @Override public void fetchCalls() { - mCallLogQueryHandler.fetchCalls(mCallTypeFilter); - ((ListsFragment) getParentFragment()).updateTabUnreadCounts(); + mCallLogQueryHandler.fetchCalls(mCallTypeFilter, mDateLimit); + if (!mIsCallLogActivity) { + ((ListsFragment) getParentFragment()).updateTabUnreadCounts(); + } } private void updateEmptyMessage(int filterType) { @@ -366,7 +438,9 @@ public class CallLogFragment extends Fragment "Unexpected filter type in CallLogFragment: " + filterType); } mEmptyListView.setDescription(messageId); - if (filterType == CallLogQueryHandler.CALL_TYPE_ALL) { + if (mIsCallLogActivity) { + mEmptyListView.setActionLabel(EmptyContentView.NO_LABEL); + } else if (filterType == CallLogQueryHandler.CALL_TYPE_ALL) { mEmptyListView.setActionLabel(R.string.call_log_all_empty_action); } } @@ -420,7 +494,7 @@ public class CallLogFragment extends Fragment if (mKeyguardManager != null && !mKeyguardManager.inKeyguardRestrictedInputMode() && mCallTypeFilter == Calls.VOICEMAIL_TYPE) { - CallLogNotificationsHelper.updateVoicemailNotifications(getActivity()); + CallLogNotificationsQueryHelper.updateVoicemailNotifications(getActivity()); } } @@ -434,7 +508,8 @@ public class CallLogFragment extends Fragment if (!PermissionsUtil.hasPermission(activity, READ_CALL_LOG)) { FragmentCompat.requestPermissions( this, new String[] {READ_CALL_LOG}, READ_CALL_LOG_PERMISSION_REQUEST_CODE); - } else { + } else if (!mIsCallLogActivity) { + // Show dialpad if we are not in the call log activity. ((HostInterface) activity).showDialpad(); } } diff --git a/java/com/android/dialer/app/calllog/CallLogListItemHelper.java b/java/com/android/dialer/app/calllog/CallLogListItemHelper.java index ea2119c83..a5df8cca1 100644 --- a/java/com/android/dialer/app/calllog/CallLogListItemHelper.java +++ b/java/com/android/dialer/app/calllog/CallLogListItemHelper.java @@ -21,18 +21,16 @@ import android.provider.CallLog.Calls; import android.support.annotation.WorkerThread; import android.text.SpannableStringBuilder; import android.text.TextUtils; -import android.util.Log; -import com.android.dialer.app.PhoneCallDetails; import com.android.dialer.app.R; import com.android.dialer.app.calllog.calllogcache.CallLogCache; +import com.android.dialer.calllogutils.PhoneCallDetails; import com.android.dialer.common.Assert; +import com.android.dialer.common.LogUtil; import com.android.dialer.compat.AppCompatConstants; /** Helper class to fill in the views of a call log entry. */ /* package */ class CallLogListItemHelper { - private static final String TAG = "CallLogListItemHelper"; - /** Helper for populating the details of a phone call. */ private final PhoneCallDetailsHelper mPhoneCallDetailsHelper; /** Resources to look up strings. */ @@ -105,7 +103,9 @@ import com.android.dialer.compat.AppCompatConstants; */ public void setActionContentDescriptions(CallLogListItemViewHolder views) { if (views.nameOrNumber == null) { - Log.e(TAG, "setActionContentDescriptions; name or number is null."); + LogUtil.e( + "CallLogListItemHelper.setActionContentDescriptions", + "setActionContentDescriptions; name or number is null."); } // Calling expandTemplate with a null parameter will cause a NullPointerException. @@ -170,7 +170,6 @@ import com.android.dialer.compat.AppCompatConstants; * *

2 calls. Answered call from John Doe mobile 1 hour ago. * - * @param context The application context. * @param details Details of call. * @return Return call action description. */ diff --git a/java/com/android/dialer/app/calllog/CallLogListItemViewHolder.java b/java/com/android/dialer/app/calllog/CallLogListItemViewHolder.java index 6abd36078..8a2d94499 100644 --- a/java/com/android/dialer/app/calllog/CallLogListItemViewHolder.java +++ b/java/com/android/dialer/app/calllog/CallLogListItemViewHolder.java @@ -25,9 +25,11 @@ import android.os.AsyncTask; import android.provider.CallLog; import android.provider.CallLog.Calls; import android.provider.ContactsContract.CommonDataKinds.Phone; +import android.support.annotation.VisibleForTesting; import android.support.v7.widget.CardView; import android.support.v7.widget.RecyclerView; import android.telecom.PhoneAccountHandle; +import android.telecom.TelecomManager; import android.telephony.PhoneNumberUtils; import android.text.BidiFormatter; import android.text.TextDirectionHeuristics; @@ -56,7 +58,7 @@ import com.android.dialer.blocking.FilteredNumberCompat; import com.android.dialer.blocking.FilteredNumbersUtil; import com.android.dialer.callcomposer.CallComposerActivity; import com.android.dialer.callcomposer.nano.CallComposerContact; -import com.android.dialer.common.ConfigProviderBindings; +import com.android.dialer.calldetails.nano.CallDetailsEntries; import com.android.dialer.common.LogUtil; import com.android.dialer.compat.CompatUtils; import com.android.dialer.logging.Logger; @@ -78,8 +80,6 @@ public final class CallLogListItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, MenuItem.OnMenuItemClickListener, View.OnCreateContextMenuListener { - private static final String CONFIG_SHARE_VOICEMAIL_ALLOWED = "share_voicemail_allowed"; - /** The root view of the call log list item */ public final View rootView; /** The quick contact badge for the contact. */ @@ -201,6 +201,7 @@ public final class CallLogListItemViewHolder extends RecyclerView.ViewHolder public boolean isAttachedToWindow; public AsyncTask asyncTask; + private CallDetailsEntries callDetailsEntries; private CallLogListItemViewHolder( Context context, @@ -549,10 +550,6 @@ public final class CallLogListItemViewHolder extends RecyclerView.ViewHolder } } - private static boolean isShareVoicemailAllowed(Context context) { - return ConfigProviderBindings.get(context).getBoolean(CONFIG_SHARE_VOICEMAIL_ALLOWED, true); - } - /** * Binds text titles, click handlers and intents to the voicemail, details and callback action * buttons. @@ -577,13 +574,14 @@ public final class CallLogListItemViewHolder extends RecyclerView.ViewHolder unblockView.setVisibility(View.GONE); reportNotSpamView.setVisibility(View.GONE); - if (isShareVoicemailAllowed(mContext)) { - sendVoicemailButtonView.setVisibility(View.VISIBLE); - } voicemailPlaybackView.setVisibility(View.VISIBLE); Uri uri = Uri.parse(voicemailUri); mVoicemailPlaybackPresenter.setPlaybackView( - voicemailPlaybackView, rowId, uri, mVoicemailPrimaryActionButtonClicked); + voicemailPlaybackView, + rowId, + uri, + mVoicemailPrimaryActionButtonClicked, + sendVoicemailButtonView); mVoicemailPrimaryActionButtonClicked = false; CallLogAsyncTaskUtil.markVoicemailAsRead(mContext, uri); return; @@ -621,14 +619,14 @@ public final class CallLogListItemViewHolder extends RecyclerView.ViewHolder && mVoicemailPlaybackPresenter != null && !TextUtils.isEmpty(voicemailUri)) { voicemailPlaybackView.setVisibility(View.VISIBLE); - if (isShareVoicemailAllowed(mContext)) { - Logger.get(mContext).logImpression(DialerImpression.Type.VVM_SHARE_VISIBLE); - sendVoicemailButtonView.setVisibility(View.VISIBLE); - } Uri uri = Uri.parse(voicemailUri); mVoicemailPlaybackPresenter.setPlaybackView( - voicemailPlaybackView, rowId, uri, mVoicemailPrimaryActionButtonClicked); + voicemailPlaybackView, + rowId, + uri, + mVoicemailPrimaryActionButtonClicked, + sendVoicemailButtonView); mVoicemailPrimaryActionButtonClicked = false; CallLogAsyncTaskUtil.markVoicemailAsRead(mContext, uri); } else { @@ -640,7 +638,8 @@ public final class CallLogListItemViewHolder extends RecyclerView.ViewHolder detailsButtonView.setVisibility(View.GONE); } else { detailsButtonView.setVisibility(View.VISIBLE); - detailsButtonView.setTag(IntentProvider.getCallDetailIntentProvider(rowId, callIds, null)); + detailsButtonView.setTag( + IntentProvider.getCallDetailIntentProvider(callDetailsEntries, buildContact())); } boolean isBlockedOrSpam = blockId != null || (isSpamFeatureEnabled && isSpam); @@ -776,6 +775,8 @@ public final class CallLogListItemViewHolder extends RecyclerView.ViewHolder contactType = ContactPhotoManager.TYPE_VOICEMAIL; } else if (isBusiness) { contactType = ContactPhotoManager.TYPE_BUSINESS; + } else if (numberPresentation == TelecomManager.PRESENTATION_RESTRICTED) { + contactType = ContactPhotoManager.TYPE_GENERIC_AVATAR; } final String lookupKey = @@ -854,20 +855,9 @@ public final class CallLogListItemViewHolder extends RecyclerView.ViewHolder } else if (view.getId() == R.id.call_compose_action) { LogUtil.i("CallLogListItemViewHolder.onClick", "share and call pressed"); Logger.get(mContext).logImpression(DialerImpression.Type.CALL_LOG_SHARE_AND_CALL); - CallComposerContact contact = new CallComposerContact(); - contact.photoId = info.photoId; - contact.photoUri = info.photoUri == null ? null : info.photoUri.toString(); - contact.contactUri = info.lookupUri == null ? null : info.lookupUri.toString(); - contact.nameOrNumber = (String) nameOrNumber; - contact.isBusiness = isBusiness; - contact.number = number; - /* second line of contact view. */ - contact.displayNumber = TextUtils.isEmpty(info.name) ? null : displayNumber; - /* phone number type (e.g. mobile) in second line of contact view */ - contact.numberLabel = numberType; Activity activity = (Activity) mContext; activity.startActivityForResult( - CallComposerActivity.newIntent(activity, contact), + CallComposerActivity.newIntent(activity, buildContact()), DialtactsActivity.ACTIVITY_REQUEST_CODE_CALL_COMPOSE); } else if (view.getId() == R.id.share_voicemail) { Logger.get(mContext).logImpression(DialerImpression.Type.VVM_SHARE_PRESSED); @@ -885,6 +875,21 @@ public final class CallLogListItemViewHolder extends RecyclerView.ViewHolder } } + private CallComposerContact buildContact() { + CallComposerContact contact = new CallComposerContact(); + contact.photoId = info.photoId; + contact.photoUri = info.photoUri == null ? null : info.photoUri.toString(); + contact.contactUri = info.lookupUri == null ? null : info.lookupUri.toString(); + contact.nameOrNumber = (String) nameOrNumber; + contact.isBusiness = isBusiness; + contact.number = number; + /* second line of contact view. */ + contact.displayNumber = TextUtils.isEmpty(info.name) ? null : displayNumber; + /* phone number type (e.g. mobile) in second line of contact view */ + contact.numberLabel = numberType; + return contact; + } + private void logCallLogAction(int id) { if (id == R.id.send_message_action) { Logger.get(mContext).logImpression(DialerImpression.Type.CALL_LOG_SEND_MESSAGE); @@ -931,6 +936,15 @@ public final class CallLogListItemViewHolder extends RecyclerView.ViewHolder } } + public void setDetailedPhoneDetails(CallDetailsEntries callDetailsEntries) { + this.callDetailsEntries = callDetailsEntries; + } + + @VisibleForTesting + public CallDetailsEntries getDetailedPhoneDetails() { + return callDetailsEntries; + } + public interface OnClickListener { void onBlockReportSpam( diff --git a/java/com/android/dialer/app/calllog/CallLogNotificationsHelper.java b/java/com/android/dialer/app/calllog/CallLogNotificationsHelper.java deleted file mode 100644 index 8f664d1a4..000000000 --- a/java/com/android/dialer/app/calllog/CallLogNotificationsHelper.java +++ /dev/null @@ -1,299 +0,0 @@ -/* - * Copyright (C) 2013 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.app.calllog; - -import android.Manifest; -import android.annotation.TargetApi; -import android.content.ContentResolver; -import android.content.ContentUris; -import android.content.Context; -import android.database.Cursor; -import android.net.Uri; -import android.os.Build.VERSION_CODES; -import android.provider.CallLog.Calls; -import android.support.annotation.Nullable; -import android.telephony.PhoneNumberUtils; -import android.text.TextUtils; -import android.util.Log; -import com.android.contacts.common.GeoUtil; -import com.android.dialer.app.R; -import com.android.dialer.phonenumbercache.ContactInfo; -import com.android.dialer.phonenumbercache.ContactInfoHelper; -import com.android.dialer.telecom.TelecomUtil; -import com.android.dialer.util.PermissionsUtil; -import java.util.ArrayList; -import java.util.List; - -/** Helper class operating on call log notifications. */ -public class CallLogNotificationsHelper { - - private static final String TAG = "CallLogNotifHelper"; - private static CallLogNotificationsHelper sInstance; - private final Context mContext; - private final NewCallsQuery mNewCallsQuery; - private final ContactInfoHelper mContactInfoHelper; - private final String mCurrentCountryIso; - - CallLogNotificationsHelper( - Context context, - NewCallsQuery newCallsQuery, - ContactInfoHelper contactInfoHelper, - String countryIso) { - mContext = context; - mNewCallsQuery = newCallsQuery; - mContactInfoHelper = contactInfoHelper; - mCurrentCountryIso = countryIso; - } - - /** Returns the singleton instance of the {@link CallLogNotificationsHelper}. */ - public static CallLogNotificationsHelper getInstance(Context context) { - if (sInstance == null) { - ContentResolver contentResolver = context.getContentResolver(); - String countryIso = GeoUtil.getCurrentCountryIso(context); - sInstance = - new CallLogNotificationsHelper( - context, - createNewCallsQuery(context, contentResolver), - new ContactInfoHelper(context, countryIso), - countryIso); - } - return sInstance; - } - - /** Removes the missed call notifications. */ - public static void removeMissedCallNotifications(Context context) { - TelecomUtil.cancelMissedCallsNotification(context); - } - - /** Update the voice mail notifications. */ - public static void updateVoicemailNotifications(Context context) { - CallLogNotificationsService.updateVoicemailNotifications(context, null); - } - - /** Create a new instance of {@link NewCallsQuery}. */ - public static NewCallsQuery createNewCallsQuery( - Context context, ContentResolver contentResolver) { - - return new DefaultNewCallsQuery(context.getApplicationContext(), contentResolver); - } - - /** - * Get all voicemails with the "new" flag set to 1. - * - * @return A list of NewCall objects where each object represents a new voicemail. - */ - @Nullable - public List getNewVoicemails() { - return mNewCallsQuery.query(Calls.VOICEMAIL_TYPE); - } - - /** - * Get all missed calls with the "new" flag set to 1. - * - * @return A list of NewCall objects where each object represents a new missed call. - */ - @Nullable - public List getNewMissedCalls() { - return mNewCallsQuery.query(Calls.MISSED_TYPE); - } - - /** - * Given a number and number information (presentation and country ISO), get the best name for - * display. If the name is empty but we have a special presentation, display that. Otherwise - * attempt to look it up in the database or the cache. If that fails, fall back to displaying the - * number. - */ - public String getName( - @Nullable String number, int numberPresentation, @Nullable String countryIso) { - return getContactInfo(number, numberPresentation, countryIso).name; - } - - /** - * Given a number and number information (presentation and country ISO), get {@link ContactInfo}. - * If the name is empty but we have a special presentation, display that. Otherwise attempt to - * look it up in the cache. If that fails, fall back to displaying the number. - */ - public ContactInfo getContactInfo( - @Nullable String number, int numberPresentation, @Nullable String countryIso) { - if (countryIso == null) { - countryIso = mCurrentCountryIso; - } - - number = (number == null) ? "" : number; - ContactInfo contactInfo = new ContactInfo(); - contactInfo.number = number; - contactInfo.formattedNumber = PhoneNumberUtils.formatNumber(number, countryIso); - // contactInfo.normalizedNumber is not PhoneNumberUtils.normalizeNumber. Read ContactInfo. - contactInfo.normalizedNumber = PhoneNumberUtils.formatNumberToE164(number, countryIso); - - // 1. Special number representation. - contactInfo.name = - PhoneNumberDisplayUtil.getDisplayName(mContext, number, numberPresentation, false) - .toString(); - if (!TextUtils.isEmpty(contactInfo.name)) { - return contactInfo; - } - - // 2. Look it up in the cache. - ContactInfo cachedContactInfo = mContactInfoHelper.lookupNumber(number, countryIso); - - if (cachedContactInfo != null && !TextUtils.isEmpty(cachedContactInfo.name)) { - return cachedContactInfo; - } - - if (!TextUtils.isEmpty(contactInfo.formattedNumber)) { - // 3. If we cannot lookup the contact, use the formatted number instead. - contactInfo.name = contactInfo.formattedNumber; - } else if (!TextUtils.isEmpty(number)) { - // 4. If number can't be formatted, use number. - contactInfo.name = number; - } else { - // 5. Otherwise, it's unknown number. - contactInfo.name = mContext.getResources().getString(R.string.unknown); - } - return contactInfo; - } - - /** Allows determining the new calls for which a notification should be generated. */ - public interface NewCallsQuery { - - /** Returns the new calls of a certain type for which a notification should be generated. */ - @Nullable - List query(int type); - } - - /** Information about a new voicemail. */ - public static final class NewCall { - - public final Uri callsUri; - public final Uri voicemailUri; - public final String number; - public final int numberPresentation; - public final String accountComponentName; - public final String accountId; - public final String transcription; - public final String countryIso; - public final long dateMs; - - public NewCall( - Uri callsUri, - Uri voicemailUri, - String number, - int numberPresentation, - String accountComponentName, - String accountId, - String transcription, - String countryIso, - long dateMs) { - this.callsUri = callsUri; - this.voicemailUri = voicemailUri; - this.number = number; - this.numberPresentation = numberPresentation; - this.accountComponentName = accountComponentName; - this.accountId = accountId; - this.transcription = transcription; - this.countryIso = countryIso; - this.dateMs = dateMs; - } - } - - /** - * Default implementation of {@link NewCallsQuery} that looks up the list of new calls to notify - * about in the call log. - */ - private static final class DefaultNewCallsQuery implements NewCallsQuery { - - private static final String[] PROJECTION = { - Calls._ID, - Calls.NUMBER, - Calls.VOICEMAIL_URI, - Calls.NUMBER_PRESENTATION, - Calls.PHONE_ACCOUNT_COMPONENT_NAME, - Calls.PHONE_ACCOUNT_ID, - Calls.TRANSCRIPTION, - Calls.COUNTRY_ISO, - Calls.DATE - }; - private static final int ID_COLUMN_INDEX = 0; - private static final int NUMBER_COLUMN_INDEX = 1; - private static final int VOICEMAIL_URI_COLUMN_INDEX = 2; - private static final int NUMBER_PRESENTATION_COLUMN_INDEX = 3; - private static final int PHONE_ACCOUNT_COMPONENT_NAME_COLUMN_INDEX = 4; - private static final int PHONE_ACCOUNT_ID_COLUMN_INDEX = 5; - private static final int TRANSCRIPTION_COLUMN_INDEX = 6; - private static final int COUNTRY_ISO_COLUMN_INDEX = 7; - private static final int DATE_COLUMN_INDEX = 8; - - private final ContentResolver mContentResolver; - private final Context mContext; - - private DefaultNewCallsQuery(Context context, ContentResolver contentResolver) { - mContext = context; - mContentResolver = contentResolver; - } - - @Override - @Nullable - @TargetApi(VERSION_CODES.M) - public List query(int type) { - if (!PermissionsUtil.hasPermission(mContext, Manifest.permission.READ_CALL_LOG)) { - Log.w(TAG, "No READ_CALL_LOG permission, returning null for calls lookup."); - return null; - } - final String selection = String.format("%s = 1 AND %s = ?", Calls.NEW, Calls.TYPE); - final String[] selectionArgs = new String[] {Integer.toString(type)}; - try (Cursor cursor = - mContentResolver.query( - Calls.CONTENT_URI_WITH_VOICEMAIL, - PROJECTION, - selection, - selectionArgs, - Calls.DEFAULT_SORT_ORDER)) { - if (cursor == null) { - return null; - } - List newCalls = new ArrayList<>(); - while (cursor.moveToNext()) { - newCalls.add(createNewCallsFromCursor(cursor)); - } - return newCalls; - } catch (RuntimeException e) { - Log.w(TAG, "Exception when querying Contacts Provider for calls lookup"); - return null; - } - } - - /** Returns an instance of {@link NewCall} created by using the values of the cursor. */ - private NewCall createNewCallsFromCursor(Cursor cursor) { - String voicemailUriString = cursor.getString(VOICEMAIL_URI_COLUMN_INDEX); - Uri callsUri = - ContentUris.withAppendedId( - Calls.CONTENT_URI_WITH_VOICEMAIL, cursor.getLong(ID_COLUMN_INDEX)); - Uri voicemailUri = voicemailUriString == null ? null : Uri.parse(voicemailUriString); - return new NewCall( - callsUri, - voicemailUri, - cursor.getString(NUMBER_COLUMN_INDEX), - cursor.getInt(NUMBER_PRESENTATION_COLUMN_INDEX), - cursor.getString(PHONE_ACCOUNT_COMPONENT_NAME_COLUMN_INDEX), - cursor.getString(PHONE_ACCOUNT_ID_COLUMN_INDEX), - cursor.getString(TRANSCRIPTION_COLUMN_INDEX), - cursor.getString(COUNTRY_ISO_COLUMN_INDEX), - cursor.getLong(DATE_COLUMN_INDEX)); - } - } -} diff --git a/java/com/android/dialer/app/calllog/CallLogNotificationsQueryHelper.java b/java/com/android/dialer/app/calllog/CallLogNotificationsQueryHelper.java new file mode 100644 index 000000000..f12837e6f --- /dev/null +++ b/java/com/android/dialer/app/calllog/CallLogNotificationsQueryHelper.java @@ -0,0 +1,334 @@ +/* + * Copyright (C) 2013 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.app.calllog; + +import android.Manifest; +import android.annotation.TargetApi; +import android.app.NotificationManager; +import android.content.ContentResolver; +import android.content.ContentUris; +import android.content.ContentValues; +import android.content.Context; +import android.database.Cursor; +import android.net.Uri; +import android.os.Build.VERSION_CODES; +import android.provider.CallLog.Calls; +import android.support.annotation.Nullable; +import android.support.annotation.WorkerThread; +import android.support.v4.os.UserManagerCompat; +import android.telephony.PhoneNumberUtils; +import android.text.TextUtils; +import com.android.contacts.common.GeoUtil; +import com.android.dialer.app.R; +import com.android.dialer.calllogutils.PhoneNumberDisplayUtil; +import com.android.dialer.common.LogUtil; +import com.android.dialer.notification.GroupedNotificationUtil; +import com.android.dialer.phonenumbercache.ContactInfo; +import com.android.dialer.phonenumbercache.ContactInfoHelper; +import com.android.dialer.util.PermissionsUtil; +import java.util.ArrayList; +import java.util.List; + +/** Helper class operating on call log notifications. */ +public class CallLogNotificationsQueryHelper { + + private static final String TAG = "CallLogNotifHelper"; + private final Context mContext; + private final NewCallsQuery mNewCallsQuery; + private final ContactInfoHelper mContactInfoHelper; + private final String mCurrentCountryIso; + + CallLogNotificationsQueryHelper( + Context context, + NewCallsQuery newCallsQuery, + ContactInfoHelper contactInfoHelper, + String countryIso) { + mContext = context; + mNewCallsQuery = newCallsQuery; + mContactInfoHelper = contactInfoHelper; + mCurrentCountryIso = countryIso; + } + + /** Returns an instance of {@link CallLogNotificationsQueryHelper}. */ + public static CallLogNotificationsQueryHelper getInstance(Context context) { + ContentResolver contentResolver = context.getContentResolver(); + String countryIso = GeoUtil.getCurrentCountryIso(context); + return new CallLogNotificationsQueryHelper( + context, + createNewCallsQuery(context, contentResolver), + new ContactInfoHelper(context, countryIso), + countryIso); + } + + /** + * Removes the missed call notifications and marks calls as read. If a callUri is provided, only + * that call is marked as read. + */ + @WorkerThread + public static void removeMissedCallNotifications(Context context, @Nullable Uri callUri) { + // Call log is only accessible when unlocked. If that's the case, clear the list of + // new missed calls from the call log. + if (UserManagerCompat.isUserUnlocked(context) && PermissionsUtil.hasPhonePermissions(context)) { + ContentValues values = new ContentValues(); + values.put(Calls.NEW, 0); + values.put(Calls.IS_READ, 1); + StringBuilder where = new StringBuilder(); + where.append(Calls.NEW); + where.append(" = 1 AND "); + where.append(Calls.TYPE); + where.append(" = ?"); + try { + context + .getContentResolver() + .update( + callUri == null ? Calls.CONTENT_URI : callUri, + values, + where.toString(), + new String[] {Integer.toString(Calls.MISSED_TYPE)}); + } catch (IllegalArgumentException e) { + LogUtil.e( + "CallLogNotificationsQueryHelper.removeMissedCallNotifications", + "contacts provider update command failed", + e); + } + } + + GroupedNotificationUtil.removeNotification( + context.getSystemService(NotificationManager.class), + callUri != null ? callUri.toString() : null, + R.id.notification_missed_call, + MissedCallNotifier.NOTIFICATION_TAG); + } + + /** Update the voice mail notifications. */ + public static void updateVoicemailNotifications(Context context) { + CallLogNotificationsService.updateVoicemailNotifications(context); + } + + /** Create a new instance of {@link NewCallsQuery}. */ + public static NewCallsQuery createNewCallsQuery( + Context context, ContentResolver contentResolver) { + + return new DefaultNewCallsQuery(context.getApplicationContext(), contentResolver); + } + + /** + * Get all voicemails with the "new" flag set to 1. + * + * @return A list of NewCall objects where each object represents a new voicemail. + */ + @Nullable + public List getNewVoicemails() { + return mNewCallsQuery.query(Calls.VOICEMAIL_TYPE); + } + + /** + * Get all missed calls with the "new" flag set to 1. + * + * @return A list of NewCall objects where each object represents a new missed call. + */ + @Nullable + public List getNewMissedCalls() { + return mNewCallsQuery.query(Calls.MISSED_TYPE); + } + + /** + * Given a number and number information (presentation and country ISO), get the best name for + * display. If the name is empty but we have a special presentation, display that. Otherwise + * attempt to look it up in the database or the cache. If that fails, fall back to displaying the + * number. + */ + public String getName( + @Nullable String number, int numberPresentation, @Nullable String countryIso) { + return getContactInfo(number, numberPresentation, countryIso).name; + } + + /** + * Given a number and number information (presentation and country ISO), get {@link ContactInfo}. + * If the name is empty but we have a special presentation, display that. Otherwise attempt to + * look it up in the cache. If that fails, fall back to displaying the number. + */ + public ContactInfo getContactInfo( + @Nullable String number, int numberPresentation, @Nullable String countryIso) { + if (countryIso == null) { + countryIso = mCurrentCountryIso; + } + + number = (number == null) ? "" : number; + ContactInfo contactInfo = new ContactInfo(); + contactInfo.number = number; + contactInfo.formattedNumber = PhoneNumberUtils.formatNumber(number, countryIso); + // contactInfo.normalizedNumber is not PhoneNumberUtils.normalizeNumber. Read ContactInfo. + contactInfo.normalizedNumber = PhoneNumberUtils.formatNumberToE164(number, countryIso); + + // 1. Special number representation. + contactInfo.name = + PhoneNumberDisplayUtil.getDisplayName(mContext, number, numberPresentation, false) + .toString(); + if (!TextUtils.isEmpty(contactInfo.name)) { + return contactInfo; + } + + // 2. Look it up in the cache. + ContactInfo cachedContactInfo = mContactInfoHelper.lookupNumber(number, countryIso); + + if (cachedContactInfo != null && !TextUtils.isEmpty(cachedContactInfo.name)) { + return cachedContactInfo; + } + + if (!TextUtils.isEmpty(contactInfo.formattedNumber)) { + // 3. If we cannot lookup the contact, use the formatted number instead. + contactInfo.name = contactInfo.formattedNumber; + } else if (!TextUtils.isEmpty(number)) { + // 4. If number can't be formatted, use number. + contactInfo.name = number; + } else { + // 5. Otherwise, it's unknown number. + contactInfo.name = mContext.getResources().getString(R.string.unknown); + } + return contactInfo; + } + + /** Allows determining the new calls for which a notification should be generated. */ + public interface NewCallsQuery { + + /** Returns the new calls of a certain type for which a notification should be generated. */ + @Nullable + List query(int type); + } + + /** Information about a new voicemail. */ + public static final class NewCall { + + public final Uri callsUri; + public final Uri voicemailUri; + public final String number; + public final int numberPresentation; + public final String accountComponentName; + public final String accountId; + public final String transcription; + public final String countryIso; + public final long dateMs; + + public NewCall( + Uri callsUri, + Uri voicemailUri, + String number, + int numberPresentation, + String accountComponentName, + String accountId, + String transcription, + String countryIso, + long dateMs) { + this.callsUri = callsUri; + this.voicemailUri = voicemailUri; + this.number = number; + this.numberPresentation = numberPresentation; + this.accountComponentName = accountComponentName; + this.accountId = accountId; + this.transcription = transcription; + this.countryIso = countryIso; + this.dateMs = dateMs; + } + } + + /** + * Default implementation of {@link NewCallsQuery} that looks up the list of new calls to notify + * about in the call log. + */ + private static final class DefaultNewCallsQuery implements NewCallsQuery { + + private static final String[] PROJECTION = { + Calls._ID, + Calls.NUMBER, + Calls.VOICEMAIL_URI, + Calls.NUMBER_PRESENTATION, + Calls.PHONE_ACCOUNT_COMPONENT_NAME, + Calls.PHONE_ACCOUNT_ID, + Calls.TRANSCRIPTION, + Calls.COUNTRY_ISO, + Calls.DATE + }; + private static final int ID_COLUMN_INDEX = 0; + private static final int NUMBER_COLUMN_INDEX = 1; + private static final int VOICEMAIL_URI_COLUMN_INDEX = 2; + private static final int NUMBER_PRESENTATION_COLUMN_INDEX = 3; + private static final int PHONE_ACCOUNT_COMPONENT_NAME_COLUMN_INDEX = 4; + private static final int PHONE_ACCOUNT_ID_COLUMN_INDEX = 5; + private static final int TRANSCRIPTION_COLUMN_INDEX = 6; + private static final int COUNTRY_ISO_COLUMN_INDEX = 7; + private static final int DATE_COLUMN_INDEX = 8; + + private final ContentResolver mContentResolver; + private final Context mContext; + + private DefaultNewCallsQuery(Context context, ContentResolver contentResolver) { + mContext = context; + mContentResolver = contentResolver; + } + + @Override + @Nullable + @TargetApi(VERSION_CODES.M) + public List query(int type) { + if (!PermissionsUtil.hasPermission(mContext, Manifest.permission.READ_CALL_LOG)) { + LogUtil.w(TAG, "No READ_CALL_LOG permission, returning null for calls lookup."); + return null; + } + final String selection = String.format("%s = 1 AND %s = ?", Calls.NEW, Calls.TYPE); + final String[] selectionArgs = new String[] {Integer.toString(type)}; + try (Cursor cursor = + mContentResolver.query( + Calls.CONTENT_URI_WITH_VOICEMAIL, + PROJECTION, + selection, + selectionArgs, + Calls.DEFAULT_SORT_ORDER)) { + if (cursor == null) { + return null; + } + List newCalls = new ArrayList<>(); + while (cursor.moveToNext()) { + newCalls.add(createNewCallsFromCursor(cursor)); + } + return newCalls; + } catch (RuntimeException e) { + LogUtil.w(TAG, "Exception when querying Contacts Provider for calls lookup"); + return null; + } + } + + /** Returns an instance of {@link NewCall} created by using the values of the cursor. */ + private NewCall createNewCallsFromCursor(Cursor cursor) { + String voicemailUriString = cursor.getString(VOICEMAIL_URI_COLUMN_INDEX); + Uri callsUri = + ContentUris.withAppendedId( + Calls.CONTENT_URI_WITH_VOICEMAIL, cursor.getLong(ID_COLUMN_INDEX)); + Uri voicemailUri = voicemailUriString == null ? null : Uri.parse(voicemailUriString); + return new NewCall( + callsUri, + voicemailUri, + cursor.getString(NUMBER_COLUMN_INDEX), + cursor.getInt(NUMBER_PRESENTATION_COLUMN_INDEX), + cursor.getString(PHONE_ACCOUNT_COMPONENT_NAME_COLUMN_INDEX), + cursor.getString(PHONE_ACCOUNT_ID_COLUMN_INDEX), + cursor.getString(TRANSCRIPTION_COLUMN_INDEX), + cursor.getString(COUNTRY_ISO_COLUMN_INDEX), + cursor.getLong(DATE_COLUMN_INDEX)); + } + } +} diff --git a/java/com/android/dialer/app/calllog/CallLogNotificationsService.java b/java/com/android/dialer/app/calllog/CallLogNotificationsService.java index 820528126..b0d48eee5 100644 --- a/java/com/android/dialer/app/calllog/CallLogNotificationsService.java +++ b/java/com/android/dialer/app/calllog/CallLogNotificationsService.java @@ -20,6 +20,7 @@ import android.app.IntentService; import android.content.Context; import android.content.Intent; import android.net.Uri; +import android.support.annotation.Nullable; import com.android.dialer.common.LogUtil; import com.android.dialer.telecom.TelecomUtil; import com.android.dialer.util.PermissionsUtil; @@ -44,20 +45,9 @@ public class CallLogNotificationsService extends IntentService { /** Action to mark all the new voicemails as old. */ public static final String ACTION_MARK_NEW_VOICEMAILS_AS_OLD = "com.android.dialer.calllog.ACTION_MARK_NEW_VOICEMAILS_AS_OLD"; - /** - * Action to update voicemail notifications. - * - *

May include an optional extra {@link #EXTRA_NEW_VOICEMAIL_URI}. - */ + /** Action to update voicemail notifications. */ public static final String ACTION_UPDATE_VOICEMAIL_NOTIFICATIONS = "com.android.dialer.calllog.UPDATE_VOICEMAIL_NOTIFICATIONS"; - /** - * Extra to included with {@link #ACTION_UPDATE_VOICEMAIL_NOTIFICATIONS} to identify the new - * voicemail that triggered an update. - * - *

It must be a {@link Uri}. - */ - public static final String EXTRA_NEW_VOICEMAIL_URI = "NEW_VOICEMAIL_URI"; /** * Action to update the missed call notifications. * @@ -66,9 +56,15 @@ public class CallLogNotificationsService extends IntentService { */ public static final String ACTION_UPDATE_MISSED_CALL_NOTIFICATIONS = "com.android.dialer.calllog.UPDATE_MISSED_CALL_NOTIFICATIONS"; + /** Action to mark all the new missed calls as old. */ public static final String ACTION_MARK_NEW_MISSED_CALLS_AS_OLD = "com.android.dialer.calllog.ACTION_MARK_NEW_MISSED_CALLS_AS_OLD"; + + /** Action to update missed call notifications with a post call note. */ + public static final String ACTION_INCOMING_POST_CALL = + "com.android.dialer.calllog.INCOMING_POST_CALL"; + /** Action to call back a missed call. */ public static final String ACTION_CALL_BACK_FROM_MISSED_CALL_NOTIFICATION = "com.android.dialer.calllog.CALL_BACK_FROM_MISSED_CALL_NOTIFICATION"; @@ -92,6 +88,21 @@ public class CallLogNotificationsService extends IntentService { */ public static final String EXTRA_MISSED_CALL_COUNT = "MISSED_CALL_COUNT"; + /** + * Extra to be included with {@link #ACTION_INCOMING_POST_CALL} to represent a post call note. + * + *

It must be a {@link String} + */ + public static final String EXTRA_POST_CALL_NOTE = "POST_CALL_NOTE"; + + /** + * Extra to be included with {@link #ACTION_INCOMING_POST_CALL} to represent the phone number the + * post call note came from. + * + *

It must be a {@link String} + */ + public static final String EXTRA_POST_CALL_NUMBER = "POST_CALL_NUMBER"; + public static final int UNKNOWN_MISSED_CALL_COUNT = -1; private VoicemailQueryHandler mVoicemailQueryHandler; @@ -103,10 +114,8 @@ public class CallLogNotificationsService extends IntentService { * Updates notifications for any new voicemails. * * @param context a valid context. - * @param voicemailUri The uri pointing to the voicemail to update the notification for. If {@code - * null}, then notifications for all new voicemails will be updated. */ - public static void updateVoicemailNotifications(Context context, Uri voicemailUri) { + public static void updateVoicemailNotifications(Context context) { if (!TelecomUtil.isDefaultDialer(context)) { LogUtil.i( "CallLogNotificationsService.updateVoicemailNotifications", @@ -116,10 +125,6 @@ public class CallLogNotificationsService extends IntentService { if (TelecomUtil.hasReadWriteVoicemailPermissions(context)) { Intent serviceIntent = new Intent(context, CallLogNotificationsService.class); serviceIntent.setAction(CallLogNotificationsService.ACTION_UPDATE_VOICEMAIL_NOTIFICATIONS); - // If voicemailUri is null, then notifications for all voicemails will be updated. - if (voicemailUri != null) { - serviceIntent.putExtra(CallLogNotificationsService.EXTRA_NEW_VOICEMAIL_URI, voicemailUri); - } context.startService(serviceIntent); } } @@ -139,9 +144,25 @@ public class CallLogNotificationsService extends IntentService { context.startService(serviceIntent); } - public static void markNewVoicemailsAsOld(Context context) { + public static void insertPostCallNote(Context context, String number, String postCallNote) { + Intent serviceIntent = new Intent(context, CallLogNotificationsService.class); + serviceIntent.setAction(ACTION_INCOMING_POST_CALL); + serviceIntent.putExtra(EXTRA_POST_CALL_NUMBER, number); + serviceIntent.putExtra(EXTRA_POST_CALL_NOTE, postCallNote); + context.startService(serviceIntent); + } + + public static void markNewVoicemailsAsOld(Context context, @Nullable Uri voicemailUri) { Intent serviceIntent = new Intent(context, CallLogNotificationsService.class); serviceIntent.setAction(CallLogNotificationsService.ACTION_MARK_NEW_VOICEMAILS_AS_OLD); + serviceIntent.setData(voicemailUri); + context.startService(serviceIntent); + } + + public static void markNewMissedCallsAsOld(Context context, @Nullable Uri callUri) { + Intent serviceIntent = new Intent(context, CallLogNotificationsService.class); + serviceIntent.setAction(ACTION_MARK_NEW_MISSED_CALLS_AS_OLD); + serviceIntent.setData(callUri); context.startService(serviceIntent); } @@ -172,11 +193,10 @@ public class CallLogNotificationsService extends IntentService { if (mVoicemailQueryHandler == null) { mVoicemailQueryHandler = new VoicemailQueryHandler(this, getContentResolver()); } - mVoicemailQueryHandler.markNewVoicemailsAsOld(); + mVoicemailQueryHandler.markNewVoicemailsAsOld(intent.getData()); break; case ACTION_UPDATE_VOICEMAIL_NOTIFICATIONS: - Uri voicemailUri = intent.getParcelableExtra(EXTRA_NEW_VOICEMAIL_URI); - DefaultVoicemailNotifier.getInstance(this).updateNotification(voicemailUri); + DefaultVoicemailNotifier.getInstance(this).updateNotification(); break; case ACTION_UPDATE_MISSED_CALL_NOTIFICATIONS: int count = intent.getIntExtra(EXTRA_MISSED_CALL_COUNT, UNKNOWN_MISSED_CALL_COUNT); @@ -184,16 +204,24 @@ public class CallLogNotificationsService extends IntentService { MissedCallNotifier.getInstance(this).updateMissedCallNotification(count, number); updateBadgeCount(this, count); break; + case ACTION_INCOMING_POST_CALL: + String note = intent.getStringExtra(EXTRA_POST_CALL_NOTE); + String phoneNumber = intent.getStringExtra(EXTRA_POST_CALL_NUMBER); + MissedCallNotifier.getInstance(this).insertPostCallNotification(phoneNumber, note); + break; case ACTION_MARK_NEW_MISSED_CALLS_AS_OLD: - CallLogNotificationsHelper.removeMissedCallNotifications(this); + CallLogNotificationsQueryHelper.removeMissedCallNotifications(this, intent.getData()); + TelecomUtil.cancelMissedCallsNotification(this); break; case ACTION_CALL_BACK_FROM_MISSED_CALL_NOTIFICATION: MissedCallNotifier.getInstance(this) - .callBackFromMissedCall(intent.getStringExtra(EXTRA_MISSED_CALL_NUMBER)); + .callBackFromMissedCall( + intent.getStringExtra(EXTRA_MISSED_CALL_NUMBER), intent.getData()); break; case ACTION_SEND_SMS_FROM_MISSED_CALL_NOTIFICATION: MissedCallNotifier.getInstance(this) - .sendSmsFromMissedCall(intent.getStringExtra(EXTRA_MISSED_CALL_NUMBER)); + .sendSmsFromMissedCall( + intent.getStringExtra(EXTRA_MISSED_CALL_NUMBER), intent.getData()); break; default: LogUtil.d("CallLogNotificationsService.onHandleIntent", "could not handle: " + intent); diff --git a/java/com/android/dialer/app/calllog/CallLogReceiver.java b/java/com/android/dialer/app/calllog/CallLogReceiver.java index a781b0887..8fd1502bc 100644 --- a/java/com/android/dialer/app/calllog/CallLogReceiver.java +++ b/java/com/android/dialer/app/calllog/CallLogReceiver.java @@ -38,9 +38,9 @@ public class CallLogReceiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { if (VoicemailContract.ACTION_NEW_VOICEMAIL.equals(intent.getAction())) { checkVoicemailStatus(context); - CallLogNotificationsService.updateVoicemailNotifications(context, intent.getData()); + CallLogNotificationsService.updateVoicemailNotifications(context); } else if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) { - CallLogNotificationsService.updateVoicemailNotifications(context, null); + CallLogNotificationsService.updateVoicemailNotifications(context); } else { LogUtil.w("CallLogReceiver.onReceive", "could not handle: " + intent); } diff --git a/java/com/android/dialer/app/calllog/CallTypeHelper.java b/java/com/android/dialer/app/calllog/CallTypeHelper.java deleted file mode 100644 index f3c27a1ac..000000000 --- a/java/com/android/dialer/app/calllog/CallTypeHelper.java +++ /dev/null @@ -1,136 +0,0 @@ -/* - * Copyright (C) 2011 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.app.calllog; - -import android.content.res.Resources; -import com.android.dialer.app.R; -import com.android.dialer.compat.AppCompatConstants; - -/** Helper class to perform operations related to call types. */ -public class CallTypeHelper { - - /** Name used to identify incoming calls. */ - private final CharSequence mIncomingName; - /** Name used to identify incoming calls which were transferred to another device. */ - private final CharSequence mIncomingPulledName; - /** Name used to identify outgoing calls. */ - private final CharSequence mOutgoingName; - /** Name used to identify outgoing calls which were transferred to another device. */ - private final CharSequence mOutgoingPulledName; - /** Name used to identify missed calls. */ - private final CharSequence mMissedName; - /** Name used to identify incoming video calls. */ - private final CharSequence mIncomingVideoName; - /** Name used to identify incoming video calls which were transferred to another device. */ - private final CharSequence mIncomingVideoPulledName; - /** Name used to identify outgoing video calls. */ - private final CharSequence mOutgoingVideoName; - /** Name used to identify outgoing video calls which were transferred to another device. */ - private final CharSequence mOutgoingVideoPulledName; - /** Name used to identify missed video calls. */ - private final CharSequence mMissedVideoName; - /** Name used to identify voicemail calls. */ - private final CharSequence mVoicemailName; - /** Name used to identify rejected calls. */ - private final CharSequence mRejectedName; - /** Name used to identify blocked calls. */ - private final CharSequence mBlockedName; - /** Name used to identify calls which were answered on another device. */ - private final CharSequence mAnsweredElsewhereName; - - public CallTypeHelper(Resources resources) { - // Cache these values so that we do not need to look them up each time. - mIncomingName = resources.getString(R.string.type_incoming); - mIncomingPulledName = resources.getString(R.string.type_incoming_pulled); - mOutgoingName = resources.getString(R.string.type_outgoing); - mOutgoingPulledName = resources.getString(R.string.type_outgoing_pulled); - mMissedName = resources.getString(R.string.type_missed); - mIncomingVideoName = resources.getString(R.string.type_incoming_video); - mIncomingVideoPulledName = resources.getString(R.string.type_incoming_video_pulled); - mOutgoingVideoName = resources.getString(R.string.type_outgoing_video); - mOutgoingVideoPulledName = resources.getString(R.string.type_outgoing_video_pulled); - mMissedVideoName = resources.getString(R.string.type_missed_video); - mVoicemailName = resources.getString(R.string.type_voicemail); - mRejectedName = resources.getString(R.string.type_rejected); - mBlockedName = resources.getString(R.string.type_blocked); - mAnsweredElsewhereName = resources.getString(R.string.type_answered_elsewhere); - } - - public static boolean isMissedCallType(int callType) { - return (callType != AppCompatConstants.CALLS_INCOMING_TYPE - && callType != AppCompatConstants.CALLS_OUTGOING_TYPE - && callType != AppCompatConstants.CALLS_VOICEMAIL_TYPE - && callType != AppCompatConstants.CALLS_ANSWERED_EXTERNALLY_TYPE); - } - - /** Returns the text used to represent the given call type. */ - public CharSequence getCallTypeText(int callType, boolean isVideoCall, boolean isPulledCall) { - switch (callType) { - case AppCompatConstants.CALLS_INCOMING_TYPE: - if (isVideoCall) { - if (isPulledCall) { - return mIncomingVideoPulledName; - } else { - return mIncomingVideoName; - } - } else { - if (isPulledCall) { - return mIncomingPulledName; - } else { - return mIncomingName; - } - } - - case AppCompatConstants.CALLS_OUTGOING_TYPE: - if (isVideoCall) { - if (isPulledCall) { - return mOutgoingVideoPulledName; - } else { - return mOutgoingVideoName; - } - } else { - if (isPulledCall) { - return mOutgoingPulledName; - } else { - return mOutgoingName; - } - } - - case AppCompatConstants.CALLS_MISSED_TYPE: - if (isVideoCall) { - return mMissedVideoName; - } else { - return mMissedName; - } - - case AppCompatConstants.CALLS_VOICEMAIL_TYPE: - return mVoicemailName; - - case AppCompatConstants.CALLS_REJECTED_TYPE: - return mRejectedName; - - case AppCompatConstants.CALLS_BLOCKED_TYPE: - return mBlockedName; - - case AppCompatConstants.CALLS_ANSWERED_EXTERNALLY_TYPE: - return mAnsweredElsewhereName; - - default: - return mMissedName; - } - } -} diff --git a/java/com/android/dialer/app/calllog/CallTypeIconsView.java b/java/com/android/dialer/app/calllog/CallTypeIconsView.java deleted file mode 100644 index cd5c5460c..000000000 --- a/java/com/android/dialer/app/calllog/CallTypeIconsView.java +++ /dev/null @@ -1,221 +0,0 @@ -/* - * Copyright (C) 2011 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.app.calllog; - -import android.content.Context; -import android.graphics.Bitmap; -import android.graphics.BitmapFactory; -import android.graphics.Canvas; -import android.graphics.PorterDuff; -import android.graphics.drawable.BitmapDrawable; -import android.graphics.drawable.Drawable; -import android.util.AttributeSet; -import android.view.View; -import com.android.contacts.common.util.BitmapUtil; -import com.android.dialer.app.R; -import com.android.dialer.compat.AppCompatConstants; -import java.util.ArrayList; -import java.util.List; - -/** - * View that draws one or more symbols for different types of calls (missed calls, outgoing etc). - * The symbols are set up horizontally. As this view doesn't create subviews, it is better suited - * for ListView-recycling that a regular LinearLayout using ImageViews. - */ -public class CallTypeIconsView extends View { - - private static Resources sResources; - private List mCallTypes = new ArrayList<>(3); - private boolean mShowVideo = false; - private int mWidth; - private int mHeight; - - public CallTypeIconsView(Context context) { - this(context, null); - } - - public CallTypeIconsView(Context context, AttributeSet attrs) { - super(context, attrs); - if (sResources == null) { - sResources = new Resources(context); - } - } - - public void clear() { - mCallTypes.clear(); - mWidth = 0; - mHeight = 0; - invalidate(); - } - - public void add(int callType) { - mCallTypes.add(callType); - - final Drawable drawable = getCallTypeDrawable(callType); - mWidth += drawable.getIntrinsicWidth() + sResources.iconMargin; - mHeight = Math.max(mHeight, drawable.getIntrinsicHeight()); - invalidate(); - } - - /** - * Determines whether the video call icon will be shown. - * - * @param showVideo True where the video icon should be shown. - */ - public void setShowVideo(boolean showVideo) { - mShowVideo = showVideo; - if (showVideo) { - mWidth += sResources.videoCall.getIntrinsicWidth(); - mHeight = Math.max(mHeight, sResources.videoCall.getIntrinsicHeight()); - invalidate(); - } - } - - /** - * Determines if the video icon should be shown. - * - * @return True if the video icon should be shown. - */ - public boolean isVideoShown() { - return mShowVideo; - } - - public int getCount() { - return mCallTypes.size(); - } - - public int getCallType(int index) { - return mCallTypes.get(index); - } - - private Drawable getCallTypeDrawable(int callType) { - switch (callType) { - case AppCompatConstants.CALLS_INCOMING_TYPE: - case AppCompatConstants.CALLS_ANSWERED_EXTERNALLY_TYPE: - return sResources.incoming; - case AppCompatConstants.CALLS_OUTGOING_TYPE: - return sResources.outgoing; - case AppCompatConstants.CALLS_MISSED_TYPE: - return sResources.missed; - case AppCompatConstants.CALLS_VOICEMAIL_TYPE: - return sResources.voicemail; - case AppCompatConstants.CALLS_BLOCKED_TYPE: - return sResources.blocked; - default: - // It is possible for users to end up with calls with unknown call types in their - // call history, possibly due to 3rd party call log implementations (e.g. to - // distinguish between rejected and missed calls). Instead of crashing, just - // assume that all unknown call types are missed calls. - return sResources.missed; - } - } - - @Override - protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { - setMeasuredDimension(mWidth, mHeight); - } - - @Override - protected void onDraw(Canvas canvas) { - int left = 0; - for (Integer callType : mCallTypes) { - final Drawable drawable = getCallTypeDrawable(callType); - final int right = left + drawable.getIntrinsicWidth(); - drawable.setBounds(left, 0, right, drawable.getIntrinsicHeight()); - drawable.draw(canvas); - left = right + sResources.iconMargin; - } - - // If showing the video call icon, draw it scaled appropriately. - if (mShowVideo) { - final Drawable drawable = sResources.videoCall; - final int right = left + sResources.videoCall.getIntrinsicWidth(); - drawable.setBounds(left, 0, right, sResources.videoCall.getIntrinsicHeight()); - drawable.draw(canvas); - } - } - - private static class Resources { - - // Drawable representing an incoming answered call. - public final Drawable incoming; - - // Drawable respresenting an outgoing call. - public final Drawable outgoing; - - // Drawable representing an incoming missed call. - public final Drawable missed; - - // Drawable representing a voicemail. - public final Drawable voicemail; - - // Drawable representing a blocked call. - public final Drawable blocked; - - // Drawable repesenting a video call. - public final Drawable videoCall; - - /** The margin to use for icons. */ - public final int iconMargin; - - /** - * Configures the call icon drawables. A single white call arrow which points down and left is - * used as a basis for all of the call arrow icons, applying rotation and colors as needed. - * - * @param context The current context. - */ - public Resources(Context context) { - final android.content.res.Resources r = context.getResources(); - - incoming = r.getDrawable(R.drawable.ic_call_arrow); - incoming.setColorFilter(r.getColor(R.color.answered_call), PorterDuff.Mode.MULTIPLY); - - // Create a rotated instance of the call arrow for outgoing calls. - outgoing = BitmapUtil.getRotatedDrawable(r, R.drawable.ic_call_arrow, 180f); - outgoing.setColorFilter(r.getColor(R.color.answered_call), PorterDuff.Mode.MULTIPLY); - - // Need to make a copy of the arrow drawable, otherwise the same instance colored - // above will be recolored here. - missed = r.getDrawable(R.drawable.ic_call_arrow).mutate(); - missed.setColorFilter(r.getColor(R.color.missed_call), PorterDuff.Mode.MULTIPLY); - - voicemail = r.getDrawable(R.drawable.quantum_ic_voicemail_white_18); - voicemail.setColorFilter( - r.getColor(R.color.dialer_secondary_text_color), PorterDuff.Mode.MULTIPLY); - - blocked = getScaledBitmap(context, R.drawable.ic_block_24dp); - blocked.setColorFilter(r.getColor(R.color.blocked_call), PorterDuff.Mode.MULTIPLY); - - videoCall = getScaledBitmap(context, R.drawable.quantum_ic_videocam_white_24); - videoCall.setColorFilter( - r.getColor(R.color.dialer_secondary_text_color), PorterDuff.Mode.MULTIPLY); - - iconMargin = r.getDimensionPixelSize(R.dimen.call_log_icon_margin); - } - - // Gets the icon, scaled to the height of the call type icons. This helps display all the - // icons to be the same height, while preserving their width aspect ratio. - private Drawable getScaledBitmap(Context context, int resourceId) { - Bitmap icon = BitmapFactory.decodeResource(context.getResources(), resourceId); - int scaledHeight = context.getResources().getDimensionPixelSize(R.dimen.call_type_icon_size); - int scaledWidth = - (int) ((float) icon.getWidth() * ((float) scaledHeight / (float) icon.getHeight())); - Bitmap scaledIcon = Bitmap.createScaledBitmap(icon, scaledWidth, scaledHeight, false); - return new BitmapDrawable(context.getResources(), scaledIcon); - } - } -} diff --git a/java/com/android/dialer/app/calllog/DefaultVoicemailNotifier.java b/java/com/android/dialer/app/calllog/DefaultVoicemailNotifier.java index 651a0ccb8..cc1dc4f20 100644 --- a/java/com/android/dialer/app/calllog/DefaultVoicemailNotifier.java +++ b/java/com/android/dialer/app/calllog/DefaultVoicemailNotifier.java @@ -19,31 +19,33 @@ package com.android.dialer.app.calllog; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; -import android.content.ComponentName; -import android.content.ContentResolver; -import android.content.ContentUris; import android.content.Context; import android.content.Intent; import android.content.res.Resources; +import android.graphics.Bitmap; import android.net.Uri; import android.os.Build.VERSION; import android.os.Build.VERSION_CODES; +import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.support.annotation.VisibleForTesting; import android.support.v4.util.Pair; -import android.telecom.PhoneAccount; import android.telecom.PhoneAccountHandle; import android.telephony.TelephonyManager; import android.text.TextUtils; import android.util.ArrayMap; -import android.util.Log; import com.android.contacts.common.compat.TelephonyManagerCompat; import com.android.contacts.common.util.ContactDisplayUtils; import com.android.dialer.app.DialtactsActivity; import com.android.dialer.app.R; -import com.android.dialer.app.calllog.CallLogNotificationsHelper.NewCall; +import com.android.dialer.app.calllog.CallLogNotificationsQueryHelper.NewCall; +import com.android.dialer.app.contactinfo.ContactPhotoLoader; import com.android.dialer.app.list.ListsFragment; import com.android.dialer.blocking.FilteredNumbersUtil; -import com.android.dialer.telecom.TelecomUtil; +import com.android.dialer.common.LogUtil; +import com.android.dialer.notification.NotificationChannelManager; +import com.android.dialer.notification.NotificationChannelManager.Channel; +import com.android.dialer.phonenumbercache.ContactInfo; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -54,26 +56,23 @@ public class DefaultVoicemailNotifier { public static final String TAG = "VoicemailNotifier"; /** The tag used to identify notifications from this class. */ - private static final String NOTIFICATION_TAG = "DefaultVoicemailNotifier"; + static final String NOTIFICATION_TAG = "DefaultVoicemailNotifier"; /** The identifier of the notification of new voicemails. */ - private static final int NOTIFICATION_ID = 1; + private static final int NOTIFICATION_ID = R.id.notification_voicemail; - /** The singleton instance of {@link DefaultVoicemailNotifier}. */ - private static DefaultVoicemailNotifier sInstance; + private final Context context; + private final CallLogNotificationsQueryHelper queryHelper; - private final Context mContext; - - private DefaultVoicemailNotifier(Context context) { - mContext = context; + @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) + DefaultVoicemailNotifier(Context context, CallLogNotificationsQueryHelper queryHelper) { + this.context = context; + this.queryHelper = queryHelper; } - /** Returns the singleton instance of the {@link DefaultVoicemailNotifier}. */ + /** Returns an instance of {@link DefaultVoicemailNotifier}. */ public static DefaultVoicemailNotifier getInstance(Context context) { - if (sInstance == null) { - ContentResolver contentResolver = context.getContentResolver(); - sInstance = new DefaultVoicemailNotifier(context); - } - return sInstance; + return new DefaultVoicemailNotifier( + context, CallLogNotificationsQueryHelper.getInstance(context)); } /** @@ -84,34 +83,23 @@ public class DefaultVoicemailNotifier { * *

It is not safe to call this method from the main thread. */ - public void updateNotification(Uri newCallUri) { + public void updateNotification() { // Lookup the list of new voicemails to include in the notification. - // TODO: Move this into a service, to avoid holding the receiver up. - final List newCalls = - CallLogNotificationsHelper.getInstance(mContext).getNewVoicemails(); + final List newCalls = queryHelper.getNewVoicemails(); if (newCalls == null) { // Query failed, just return. return; } - if (newCalls.isEmpty()) { - // No voicemails to notify about: clear the notification. - getNotificationManager().cancel(NOTIFICATION_TAG, NOTIFICATION_ID); - return; - } - - Resources resources = mContext.getResources(); + Resources resources = context.getResources(); // This represents a list of names to include in the notification. String callers = null; // Maps each number into a name: if a number is in the map, it has already left a more // recent voicemail. - final Map names = new ArrayMap<>(); - - // Determine the call corresponding to the new voicemail we have to notify about. - NewCall callToNotify = null; + final Map contactInfos = new ArrayMap<>(); // Iterate over the new voicemails to determine all the information above. Iterator itr = newCalls.iterator(); @@ -120,95 +108,64 @@ public class DefaultVoicemailNotifier { // Skip notifying for numbers which are blocked. if (FilteredNumbersUtil.shouldBlockVoicemail( - mContext, newCall.number, newCall.countryIso, newCall.dateMs)) { + context, newCall.number, newCall.countryIso, newCall.dateMs)) { itr.remove(); // Delete the voicemail. - mContext.getContentResolver().delete(newCall.voicemailUri, null, null); + context.getContentResolver().delete(newCall.voicemailUri, null, null); continue; } // Check if we already know the name associated with this number. - String name = names.get(newCall.number); - if (name == null) { - name = - CallLogNotificationsHelper.getInstance(mContext) - .getName(newCall.number, newCall.numberPresentation, newCall.countryIso); - names.put(newCall.number, name); + ContactInfo contactInfo = contactInfos.get(newCall.number); + if (contactInfo == null) { + contactInfo = + queryHelper.getContactInfo( + newCall.number, newCall.numberPresentation, newCall.countryIso); + contactInfos.put(newCall.number, contactInfo); // This is a new caller. Add it to the back of the list of callers. if (TextUtils.isEmpty(callers)) { - callers = name; + callers = contactInfo.name; } else { callers = - resources.getString(R.string.notification_voicemail_callers_list, callers, name); + resources.getString( + R.string.notification_voicemail_callers_list, callers, contactInfo.name); } } - // Check if this is the new call we need to notify about. - if (newCallUri != null - && newCall.voicemailUri != null - && ContentUris.parseId(newCallUri) == ContentUris.parseId(newCall.voicemailUri)) { - callToNotify = newCall; - } } - // All the potential new voicemails have been removed, e.g. if they were spam. if (newCalls.isEmpty()) { + // No voicemails to notify about: clear the notification. + CallLogNotificationsService.markNewVoicemailsAsOld(context, null); return; } - // If there is only one voicemail, set its transcription as the "long text". - String transcription = null; - if (newCalls.size() == 1) { - transcription = newCalls.get(0).transcription; - } - - if (newCallUri != null && callToNotify == null) { - Log.e(TAG, "The new call could not be found in the call log: " + newCallUri); - } - - // Determine the title of the notification and the icon for it. - final String title = - resources.getQuantityString( - R.plurals.notification_voicemail_title, newCalls.size(), newCalls.size()); - // TODO: Use the photo of contact if all calls are from the same person. - final int icon = android.R.drawable.stat_notify_voicemail; - - Pair info = getNotificationInfo(callToNotify); - - Notification.Builder notificationBuilder = - new Notification.Builder(mContext) - .setSmallIcon(icon) - .setContentTitle(title) + Notification.Builder groupSummary = + createNotificationBuilder() + .setContentTitle( + resources.getQuantityString( + R.plurals.notification_voicemail_title, newCalls.size(), newCalls.size())) .setContentText(callers) - .setColor(resources.getColor(R.color.dialer_theme_color)) - .setSound(info.first) - .setDefaults(info.second) - .setDeleteIntent(createMarkNewVoicemailsAsOldIntent()) - .setAutoCancel(true); - - if (!TextUtils.isEmpty(transcription)) { - notificationBuilder.setStyle(new Notification.BigTextStyle().bigText(transcription)); + .setDeleteIntent(createMarkNewVoicemailsAsOldIntent(null)) + .setGroupSummary(true) + .setContentIntent(newVoicemailIntent(null)); + + NotificationChannelManager.applyChannel( + groupSummary, + context, + Channel.VOICEMAIL, + PhoneAccountHandles.getAccount(context, newCalls.get(0))); + + LogUtil.i(TAG, "Creating voicemail notification"); + getNotificationManager().notify(NOTIFICATION_TAG, NOTIFICATION_ID, groupSummary.build()); + + for (NewCall voicemail : newCalls) { + getNotificationManager() + .notify( + voicemail.voicemailUri.toString(), + NOTIFICATION_ID, + createNotificationForVoicemail(voicemail, contactInfos)); } - - // Determine the intent to fire when the notification is clicked on. - final Intent contentIntent; - // Open the call log. - contentIntent = DialtactsActivity.getShowTabIntent(mContext, ListsFragment.TAB_INDEX_VOICEMAIL); - contentIntent.putExtra(DialtactsActivity.EXTRA_CLEAR_NEW_VOICEMAILS, true); - notificationBuilder.setContentIntent( - PendingIntent.getActivity(mContext, 0, contentIntent, PendingIntent.FLAG_UPDATE_CURRENT)); - - // The text to show in the ticker, describing the new event. - if (callToNotify != null) { - CharSequence msg = - ContactDisplayUtils.getTtsSpannedPhoneNumber( - resources, - R.string.notification_new_voicemail_ticker, - names.get(callToNotify.number)); - notificationBuilder.setTicker(msg); - } - Log.i(TAG, "Creating voicemail notification"); - getNotificationManager().notify(NOTIFICATION_TAG, NOTIFICATION_ID, notificationBuilder.build()); } /** @@ -216,30 +173,15 @@ public class DefaultVoicemailNotifier { * for the given call. */ private Pair getNotificationInfo(@Nullable NewCall callToNotify) { - Log.v(TAG, "getNotificationInfo"); + LogUtil.v(TAG, "getNotificationInfo"); if (callToNotify == null) { - Log.i(TAG, "callToNotify == null"); + LogUtil.i(TAG, "callToNotify == null"); return new Pair<>(null, 0); } - PhoneAccountHandle accountHandle; - if (callToNotify.accountComponentName == null || callToNotify.accountId == null) { - Log.v(TAG, "accountComponentName == null || callToNotify.accountId == null"); - accountHandle = TelecomUtil.getDefaultOutgoingPhoneAccount(mContext, PhoneAccount.SCHEME_TEL); - if (accountHandle == null) { - Log.i(TAG, "No default phone account found, using default notification ringtone"); - return new Pair<>(null, Notification.DEFAULT_ALL); - } - - } else { - accountHandle = - new PhoneAccountHandle( - ComponentName.unflattenFromString(callToNotify.accountComponentName), - callToNotify.accountId); - } - if (accountHandle.getComponentName() != null) { - Log.v(TAG, "PhoneAccountHandle.ComponentInfo:" + accountHandle.getComponentName()); - } else { - Log.i(TAG, "PhoneAccountHandle.ComponentInfo: null"); + PhoneAccountHandle accountHandle = PhoneAccountHandles.getAccount(context, callToNotify); + if (accountHandle == null) { + LogUtil.i(TAG, "No default phone account found, using default notification ringtone"); + return new Pair<>(null, Notification.DEFAULT_ALL); } return new Pair<>( TelephonyManagerCompat.getVoicemailRingtoneUri(getTelephonyManager(), accountHandle), @@ -257,17 +199,79 @@ public class DefaultVoicemailNotifier { } /** Creates a pending intent that marks all new voicemails as old. */ - private PendingIntent createMarkNewVoicemailsAsOldIntent() { - Intent intent = new Intent(mContext, CallLogNotificationsService.class); + private PendingIntent createMarkNewVoicemailsAsOldIntent(@Nullable Uri voicemailUri) { + Intent intent = new Intent(context, CallLogNotificationsService.class); intent.setAction(CallLogNotificationsService.ACTION_MARK_NEW_VOICEMAILS_AS_OLD); - return PendingIntent.getService(mContext, 0, intent, 0); + intent.setData(voicemailUri); + return PendingIntent.getService(context, 0, intent, 0); } private NotificationManager getNotificationManager() { - return (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); + return (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); } private TelephonyManager getTelephonyManager() { - return (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE); + return (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); + } + + private Notification createNotificationForVoicemail( + @NonNull NewCall voicemail, @NonNull Map contactInfos) { + Pair notificationInfo = getNotificationInfo(voicemail); + ContactInfo contactInfo = contactInfos.get(voicemail.number); + + Notification.Builder notificationBuilder = + createNotificationBuilder() + .setContentTitle( + context + .getResources() + .getQuantityString(R.plurals.notification_voicemail_title, 1, 1)) + .setContentText( + ContactDisplayUtils.getTtsSpannedPhoneNumber( + context.getResources(), + R.string.notification_new_voicemail_ticker, + contactInfo.name)) + .setWhen(voicemail.dateMs) + .setSound(notificationInfo.first) + .setDefaults(notificationInfo.second) + .setDeleteIntent(createMarkNewVoicemailsAsOldIntent(voicemail.voicemailUri)); + + NotificationChannelManager.applyChannel( + notificationBuilder, + context, + Channel.VOICEMAIL, + PhoneAccountHandles.getAccount(context, voicemail)); + + ContactPhotoLoader loader = new ContactPhotoLoader(context, contactInfo); + Bitmap photoIcon = loader.loadPhotoIcon(); + if (photoIcon != null) { + notificationBuilder.setLargeIcon(photoIcon); + } + + if (!TextUtils.isEmpty(voicemail.transcription)) { + notificationBuilder.setStyle( + new Notification.BigTextStyle().bigText(voicemail.transcription)); + } + notificationBuilder.setContentIntent(newVoicemailIntent(voicemail)); + + return notificationBuilder.build(); + } + + private Notification.Builder createNotificationBuilder() { + return new Notification.Builder(context) + .setSmallIcon(android.R.drawable.stat_notify_voicemail) + .setColor(context.getColor(R.color.dialer_theme_color)) + .setGroup(NOTIFICATION_TAG) + .setOnlyAlertOnce(true) + .setAutoCancel(true); + } + + private PendingIntent newVoicemailIntent(@Nullable NewCall voicemail) { + Intent intent = DialtactsActivity.getShowTabIntent(context, ListsFragment.TAB_INDEX_VOICEMAIL); + // TODO (b/35486204): scroll to this voicemail + if (voicemail != null) { + intent.setData(voicemail.voicemailUri); + } + intent.putExtra(DialtactsActivity.EXTRA_CLEAR_NEW_VOICEMAILS, true); + return PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); } } diff --git a/java/com/android/dialer/app/calllog/IntentProvider.java b/java/com/android/dialer/app/calllog/IntentProvider.java index 879ac353d..c53e3ec5e 100644 --- a/java/com/android/dialer/app/calllog/IntentProvider.java +++ b/java/com/android/dialer/app/calllog/IntentProvider.java @@ -16,7 +16,6 @@ package com.android.dialer.app.calllog; -import android.content.ContentUris; import android.content.ContentValues; import android.content.Context; import android.content.Intent; @@ -25,10 +24,11 @@ import android.provider.ContactsContract; import android.telecom.PhoneAccountHandle; import com.android.contacts.common.model.Contact; import com.android.contacts.common.model.ContactLoader; -import com.android.dialer.app.CallDetailActivity; +import com.android.dialer.callcomposer.nano.CallComposerContact; +import com.android.dialer.calldetails.CallDetailsActivity; +import com.android.dialer.calldetails.nano.CallDetailsEntries; import com.android.dialer.callintent.CallIntentBuilder; import com.android.dialer.callintent.nano.CallInitiationType; -import com.android.dialer.telecom.TelecomUtil; import com.android.dialer.util.CallUtil; import com.android.dialer.util.IntentUtil; import java.util.ArrayList; @@ -97,29 +97,16 @@ public abstract class IntentProvider { /** * Retrieves the call details intent provider for an entry in the call log. * - * @param id The call ID of the first call in the call group. - * @param extraIds The call ID of the other calls grouped together with the call. - * @param voicemailUri If call log entry is for a voicemail, the voicemail URI. + * @param callDetailsEntries The call details of the other calls grouped together with the call. + * @param contact The contact with which this call details intent pertains to. * @return The call details intent provider. */ public static IntentProvider getCallDetailIntentProvider( - final long id, final long[] extraIds, final String voicemailUri) { + CallDetailsEntries callDetailsEntries, CallComposerContact contact) { return new IntentProvider() { @Override public Intent getIntent(Context context) { - Intent intent = new Intent(context, CallDetailActivity.class); - // Check if the first item is a voicemail. - if (voicemailUri != null) { - intent.putExtra(CallDetailActivity.EXTRA_VOICEMAIL_URI, Uri.parse(voicemailUri)); - } - - if (extraIds != null && extraIds.length > 0) { - intent.putExtra(CallDetailActivity.EXTRA_CALL_LOG_IDS, extraIds); - } else { - // If there is a single item, use the direct URI for it. - intent.setData(ContentUris.withAppendedId(TelecomUtil.getCallLogUri(context), id)); - } - return intent; + return CallDetailsActivity.newInstance(context, callDetailsEntries, contact); } }; } diff --git a/java/com/android/dialer/app/calllog/MissedCallNotifier.java b/java/com/android/dialer/app/calllog/MissedCallNotifier.java index 2fa3dae65..5b5661615 100644 --- a/java/com/android/dialer/app/calllog/MissedCallNotifier.java +++ b/java/com/android/dialer/app/calllog/MissedCallNotifier.java @@ -16,16 +16,20 @@ package com.android.dialer.app.calllog; import android.app.Notification; +import android.app.Notification.Builder; import android.app.NotificationManager; import android.app.PendingIntent; -import android.content.ContentValues; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; -import android.os.AsyncTask; +import android.graphics.drawable.Icon; +import android.net.Uri; import android.provider.CallLog.Calls; +import android.service.notification.StatusBarNotification; +import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.annotation.VisibleForTesting; +import android.support.annotation.WorkerThread; import android.support.v4.os.UserManagerCompat; import android.text.BidiFormatter; import android.text.TextDirectionHeuristics; @@ -34,109 +38,117 @@ import com.android.contacts.common.ContactsUtils; import com.android.contacts.common.compat.PhoneNumberUtilsCompat; import com.android.dialer.app.DialtactsActivity; import com.android.dialer.app.R; -import com.android.dialer.app.calllog.CallLogNotificationsHelper.NewCall; +import com.android.dialer.app.calllog.CallLogNotificationsQueryHelper.NewCall; import com.android.dialer.app.contactinfo.ContactPhotoLoader; import com.android.dialer.app.list.ListsFragment; import com.android.dialer.callintent.CallIntentBuilder; import com.android.dialer.callintent.nano.CallInitiationType; -import com.android.dialer.common.ConfigProviderBindings; import com.android.dialer.common.LogUtil; +import com.android.dialer.notification.NotificationChannelManager; +import com.android.dialer.notification.NotificationChannelManager.Channel; import com.android.dialer.phonenumbercache.ContactInfo; import com.android.dialer.phonenumberutil.PhoneNumberHelper; +import com.android.dialer.telecom.TelecomUtil; import com.android.dialer.util.DialerUtils; import com.android.dialer.util.IntentUtil; +import java.util.HashSet; import java.util.List; +import java.util.Set; /** Creates a notification for calls that the user missed (neither answered nor rejected). */ public class MissedCallNotifier { /** The tag used to identify notifications from this class. */ - private static final String NOTIFICATION_TAG = "MissedCallNotifier"; + static final String NOTIFICATION_TAG = "MissedCallNotifier"; /** The identifier of the notification of new missed calls. */ - private static final int NOTIFICATION_ID = 1; + private static final int NOTIFICATION_ID = R.id.notification_missed_call; - private static MissedCallNotifier sInstance; - private Context mContext; - private CallLogNotificationsHelper mCalllogNotificationsHelper; + private final Context context; + private final CallLogNotificationsQueryHelper callLogNotificationsQueryHelper; @VisibleForTesting - MissedCallNotifier(Context context, CallLogNotificationsHelper callLogNotificationsHelper) { - mContext = context; - mCalllogNotificationsHelper = callLogNotificationsHelper; + MissedCallNotifier( + Context context, CallLogNotificationsQueryHelper callLogNotificationsQueryHelper) { + this.context = context; + this.callLogNotificationsQueryHelper = callLogNotificationsQueryHelper; } - /** Returns the singleton instance of the {@link MissedCallNotifier}. */ + /** Returns an instance of {@link MissedCallNotifier}. */ public static MissedCallNotifier getInstance(Context context) { - if (sInstance == null) { - CallLogNotificationsHelper callLogNotificationsHelper = - CallLogNotificationsHelper.getInstance(context); - sInstance = new MissedCallNotifier(context, callLogNotificationsHelper); - } - return sInstance; + CallLogNotificationsQueryHelper callLogNotificationsQueryHelper = + CallLogNotificationsQueryHelper.getInstance(context); + return new MissedCallNotifier(context, callLogNotificationsQueryHelper); } /** - * Creates a missed call notification with a post call message if there are no existing missed - * calls. + * Update missed call notifications from the call log. Accepts default information in case call + * log cannot be accessed. + * + * @param count the number of missed calls to display if call log cannot be accessed. May be + * {@link CallLogNotificationsService#UNKNOWN_MISSED_CALL_COUNT} if unknown. + * @param number the phone number of the most recent call to display if the call log cannot be + * accessed. May be null if unknown. */ - public void createPostCallMessageNotification(String number, String message) { - int count = CallLogNotificationsService.UNKNOWN_MISSED_CALL_COUNT; - if (ConfigProviderBindings.get(mContext).getBoolean("enable_call_compose", false)) { - updateMissedCallNotification(count, number, message); - } else { - updateMissedCallNotification(count, number, null); - } - } - - /** Creates a missed call notification. */ - public void updateMissedCallNotification(int count, String number) { - updateMissedCallNotification(count, number, null); - } - - private void updateMissedCallNotification( - int count, String number, @Nullable String postCallMessage) { + @WorkerThread + public void updateMissedCallNotification(int count, @Nullable String number) { final int titleResId; CharSequence expandedText; // The text in the notification's line 1 and 2. - final List newCalls = mCalllogNotificationsHelper.getNewMissedCalls(); + List newCalls = callLogNotificationsQueryHelper.getNewMissedCalls(); - if (count == CallLogNotificationsService.UNKNOWN_MISSED_CALL_COUNT) { - if (newCalls == null) { - // If the intent did not contain a count, and we are unable to get a count from the - // call log, then no notification can be shown. - return; + if ((newCalls != null && newCalls.isEmpty()) || count == 0) { + // No calls to notify about: clear the notification. + CallLogNotificationsQueryHelper.removeMissedCallNotifications(context, null); + return; + } + + if (newCalls != null) { + if (count != CallLogNotificationsService.UNKNOWN_MISSED_CALL_COUNT + && count != newCalls.size()) { + LogUtil.w( + "MissedCallNotifier.updateMissedCallNotification", + "Call count does not match call log count." + + " count: " + + count + + " newCalls.size(): " + + newCalls.size()); } count = newCalls.size(); } - if (count == 0) { - // No voicemails to notify about: clear the notification. - clearMissedCalls(); + if (count == CallLogNotificationsService.UNKNOWN_MISSED_CALL_COUNT) { + // If the intent did not contain a count, and we are unable to get a count from the + // call log, then no notification can be shown. return; } - // The call log has been updated, use that information preferentially. - boolean useCallLog = newCalls != null && newCalls.size() == count; - NewCall newestCall = useCallLog ? newCalls.get(0) : null; - long timeMs = useCallLog ? newestCall.dateMs : System.currentTimeMillis(); - String missedNumber = useCallLog ? newestCall.number : number; + Notification.Builder groupSummary = createNotificationBuilder(); + boolean useCallList = newCalls != null; - Notification.Builder builder = new Notification.Builder(mContext); - // Display the first line of the notification: - // 1 missed call: - // More than 1 missed call: + "missed calls" if (count == 1) { + NewCall call = + useCallList + ? newCalls.get(0) + : new NewCall( + null, + null, + number, + Calls.PRESENTATION_ALLOWED, + null, + null, + null, + null, + System.currentTimeMillis()); + //TODO: look up caller ID that is not in contacts. ContactInfo contactInfo = - mCalllogNotificationsHelper.getContactInfo( - missedNumber, - useCallLog ? newestCall.numberPresentation : Calls.PRESENTATION_ALLOWED, - useCallLog ? newestCall.countryIso : null); - + callLogNotificationsQueryHelper.getContactInfo( + call.number, call.numberPresentation, call.countryIso); titleResId = contactInfo.userType == ContactsUtils.USER_TYPE_WORK ? R.string.notification_missedWorkCallTitle : R.string.notification_missedCallTitle; + if (TextUtils.equals(contactInfo.name, contactInfo.formattedNumber) || TextUtils.equals(contactInfo.name, contactInfo.number)) { expandedText = @@ -147,134 +159,195 @@ public class MissedCallNotifier { expandedText = contactInfo.name; } - if (!TextUtils.isEmpty(postCallMessage)) { - // Ex. "John Doe: Hey dude" - expandedText = - mContext.getString( - R.string.post_call_notification_message, expandedText, postCallMessage); - } - ContactPhotoLoader loader = new ContactPhotoLoader(mContext, contactInfo); + ContactPhotoLoader loader = new ContactPhotoLoader(context, contactInfo); Bitmap photoIcon = loader.loadPhotoIcon(); if (photoIcon != null) { - builder.setLargeIcon(photoIcon); + groupSummary.setLargeIcon(photoIcon); } } else { titleResId = R.string.notification_missedCallsTitle; - expandedText = mContext.getString(R.string.notification_missedCallsMsg, count); + expandedText = context.getString(R.string.notification_missedCallsMsg, count); } // Create a public viewable version of the notification, suitable for display when sensitive // notification content is hidden. - Notification.Builder publicBuilder = new Notification.Builder(mContext); - publicBuilder - .setSmallIcon(android.R.drawable.stat_notify_missed_call) - .setColor(mContext.getResources().getColor(R.color.dialer_theme_color)) - // Show "Phone" for notification title. - .setContentTitle(mContext.getText(R.string.userCallActivityLabel)) - // Notification details shows that there are missed call(s), but does not reveal - // the missed caller information. - .setContentText(mContext.getText(titleResId)) + Notification.Builder publicSummaryBuilder = createNotificationBuilder(); + publicSummaryBuilder + .setContentTitle(context.getText(titleResId)) .setContentIntent(createCallLogPendingIntent()) - .setAutoCancel(true) - .setWhen(timeMs) - .setShowWhen(true) - .setDeleteIntent(createClearMissedCallsPendingIntent()); + .setDeleteIntent(createClearMissedCallsPendingIntent(null)); + // Create the notification summary suitable for display when sensitive information is showing. + groupSummary + .setContentTitle(context.getText(titleResId)) + .setContentText(expandedText) + .setContentIntent(createCallLogPendingIntent()) + .setDeleteIntent(createClearMissedCallsPendingIntent(null)) + .setGroupSummary(useCallList) + .setOnlyAlertOnce(useCallList) + .setPublicVersion(publicSummaryBuilder.build()); + + NotificationChannelManager.applyChannel( + groupSummary, + context, + Channel.MISSED_CALL, + PhoneAccountHandles.getAccount(context, useCallList ? newCalls.get(0) : null)); + + Notification notification = groupSummary.build(); + configureLedOnNotification(notification); + + LogUtil.i("MissedCallNotifier.updateMissedCallNotification", "adding missed call notification"); + getNotificationMgr().notify(NOTIFICATION_TAG, NOTIFICATION_ID, notification); + + if (useCallList) { + // Do not repost active notifications to prevent erasing post call notes. + NotificationManager manager = getNotificationMgr(); + Set activeTags = new HashSet<>(); + for (StatusBarNotification activeNotification : manager.getActiveNotifications()) { + activeTags.add(activeNotification.getTag()); + } + + for (NewCall call : newCalls) { + String callTag = call.callsUri.toString(); + if (!activeTags.contains(callTag)) { + manager.notify(callTag, NOTIFICATION_ID, getNotificationForCall(call, null)); + } + } + } + } + + public void insertPostCallNotification(@NonNull String number, @NonNull String note) { + List newCalls = callLogNotificationsQueryHelper.getNewMissedCalls(); + if (newCalls != null && !newCalls.isEmpty()) { + for (NewCall call : newCalls) { + if (call.number.equals(number.replace("tel:", ""))) { + // Update the first notification that matches our post call note sender. + getNotificationMgr() + .notify( + call.callsUri.toString(), NOTIFICATION_ID, getNotificationForCall(call, note)); + break; + } + } + } + } + + private Notification getNotificationForCall( + @NonNull NewCall call, @Nullable String postCallMessage) { + ContactInfo contactInfo = + callLogNotificationsQueryHelper.getContactInfo( + call.number, call.numberPresentation, call.countryIso); + + // Create a public viewable version of the notification, suitable for display when sensitive + // notification content is hidden. + int titleResId = + contactInfo.userType == ContactsUtils.USER_TYPE_WORK + ? R.string.notification_missedWorkCallTitle + : R.string.notification_missedCallTitle; + Notification.Builder publicBuilder = + createNotificationBuilder(call).setContentTitle(context.getText(titleResId)); + + Notification.Builder builder = createNotificationBuilder(call); + CharSequence expandedText; + if (TextUtils.equals(contactInfo.name, contactInfo.formattedNumber) + || TextUtils.equals(contactInfo.name, contactInfo.number)) { + expandedText = + PhoneNumberUtilsCompat.createTtsSpannable( + BidiFormatter.getInstance() + .unicodeWrap(contactInfo.name, TextDirectionHeuristics.LTR)); + } else { + expandedText = contactInfo.name; + } + + if (postCallMessage != null) { + expandedText = + context.getString(R.string.post_call_notification_message, expandedText, postCallMessage); + } + + ContactPhotoLoader loader = new ContactPhotoLoader(context, contactInfo); + Bitmap photoIcon = loader.loadPhotoIcon(); + if (photoIcon != null) { + builder.setLargeIcon(photoIcon); + } // Create the notification suitable for display when sensitive information is showing. builder - .setSmallIcon(android.R.drawable.stat_notify_missed_call) - .setColor(mContext.getResources().getColor(R.color.dialer_theme_color)) - .setContentTitle(mContext.getText(titleResId)) + .setContentTitle(context.getText(titleResId)) .setContentText(expandedText) - .setContentIntent(createCallLogPendingIntent()) - .setAutoCancel(true) - .setWhen(timeMs) - .setShowWhen(true) - .setDefaults(Notification.DEFAULT_VIBRATE) - .setDeleteIntent(createClearMissedCallsPendingIntent()) // Include a public version of the notification to be shown when the missed call // notification is shown on the user's lock screen and they have chosen to hide // sensitive notification information. .setPublicVersion(publicBuilder.build()); - // Add additional actions when there is only 1 missed call and the user isn't locked - if (UserManagerCompat.isUserUnlocked(mContext) && count == 1) { - if (!TextUtils.isEmpty(missedNumber) - && !TextUtils.equals(missedNumber, mContext.getString(R.string.handle_restricted))) { + // Add additional actions when the user isn't locked + if (UserManagerCompat.isUserUnlocked(context)) { + if (!TextUtils.isEmpty(call.number) + && !TextUtils.equals(call.number, context.getString(R.string.handle_restricted))) { builder.addAction( - R.drawable.ic_phone_24dp, - mContext.getString(R.string.notification_missedCall_call_back), - createCallBackPendingIntent(missedNumber)); + new Notification.Action.Builder( + Icon.createWithResource(context, R.drawable.ic_phone_24dp), + context.getString(R.string.notification_missedCall_call_back), + createCallBackPendingIntent(call.number, call.callsUri)) + .build()); - if (!PhoneNumberHelper.isUriNumber(missedNumber)) { + if (!PhoneNumberHelper.isUriNumber(call.number)) { builder.addAction( - R.drawable.ic_message_24dp, - mContext.getString(R.string.notification_missedCall_message), - createSendSmsFromNotificationPendingIntent(missedNumber)); + new Notification.Action.Builder( + Icon.createWithResource(context, R.drawable.ic_message_24dp), + context.getString(R.string.notification_missedCall_message), + createSendSmsFromNotificationPendingIntent(call.number, call.callsUri)) + .build()); } } } Notification notification = builder.build(); configureLedOnNotification(notification); + return notification; + } - LogUtil.i("MissedCallNotifier.updateMissedCallNotification", "adding missed call notification"); - getNotificationMgr().notify(NOTIFICATION_TAG, NOTIFICATION_ID, notification); + private Notification.Builder createNotificationBuilder() { + return new Notification.Builder(context) + .setGroup(NOTIFICATION_TAG) + .setSmallIcon(android.R.drawable.stat_notify_missed_call) + .setColor(context.getResources().getColor(R.color.dialer_theme_color, null)) + .setAutoCancel(true) + .setOnlyAlertOnce(true) + .setShowWhen(true) + .setDefaults(Notification.DEFAULT_VIBRATE); } - private void clearMissedCalls() { - AsyncTask.execute( - new Runnable() { - @Override - public void run() { - // Call log is only accessible when unlocked. If that's the case, clear the list of - // new missed calls from the call log. - if (UserManagerCompat.isUserUnlocked(mContext)) { - ContentValues values = new ContentValues(); - values.put(Calls.NEW, 0); - values.put(Calls.IS_READ, 1); - StringBuilder where = new StringBuilder(); - where.append(Calls.NEW); - where.append(" = 1 AND "); - where.append(Calls.TYPE); - where.append(" = ?"); - try { - mContext - .getContentResolver() - .update( - Calls.CONTENT_URI, - values, - where.toString(), - new String[] {Integer.toString(Calls.MISSED_TYPE)}); - } catch (IllegalArgumentException e) { - LogUtil.e( - "MissedCallNotifier.clearMissedCalls", - "contacts provider update command failed", - e); - } - } - getNotificationMgr().cancel(NOTIFICATION_TAG, NOTIFICATION_ID); - } - }); + private Notification.Builder createNotificationBuilder(@NonNull NewCall call) { + Builder builder = + createNotificationBuilder() + .setWhen(call.dateMs) + .setDeleteIntent(createClearMissedCallsPendingIntent(call.callsUri)) + .setContentIntent(createCallLogPendingIntent(call.callsUri)); + + NotificationChannelManager.applyChannel( + builder, context, Channel.MISSED_CALL, PhoneAccountHandles.getAccount(context, call)); + return builder; } /** Trigger an intent to make a call from a missed call number. */ - public void callBackFromMissedCall(String number) { - closeSystemDialogs(mContext); - CallLogNotificationsHelper.removeMissedCallNotifications(mContext); + @WorkerThread + public void callBackFromMissedCall(String number, Uri callUri) { + closeSystemDialogs(context); + CallLogNotificationsQueryHelper.removeMissedCallNotifications(context, callUri); + TelecomUtil.cancelMissedCallsNotification(context); DialerUtils.startActivityWithErrorToast( - mContext, + context, new CallIntentBuilder(number, CallInitiationType.Type.MISSED_CALL_NOTIFICATION) .build() .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)); } /** Trigger an intent to send an sms from a missed call number. */ - public void sendSmsFromMissedCall(String number) { - closeSystemDialogs(mContext); - CallLogNotificationsHelper.removeMissedCallNotifications(mContext); + @WorkerThread + public void sendSmsFromMissedCall(String number, Uri callUri) { + closeSystemDialogs(context); + CallLogNotificationsQueryHelper.removeMissedCallNotifications(context, callUri); + TelecomUtil.cancelMissedCallsNotification(context); DialerUtils.startActivityWithErrorToast( - mContext, IntentUtil.getSendSmsIntent(number).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)); + context, IntentUtil.getSendSmsIntent(number).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)); } /** @@ -283,34 +356,50 @@ public class MissedCallNotifier { * @return The pending intent. */ private PendingIntent createCallLogPendingIntent() { + return createCallLogPendingIntent(null); + } + + /** + * Creates a new pending intent that sends the user to the call log. + * + * @return The pending intent. + * @param callUri Uri of the call to jump to. May be null + */ + private PendingIntent createCallLogPendingIntent(@Nullable Uri callUri) { Intent contentIntent = - DialtactsActivity.getShowTabIntent(mContext, ListsFragment.TAB_INDEX_HISTORY); - return PendingIntent.getActivity(mContext, 0, contentIntent, PendingIntent.FLAG_UPDATE_CURRENT); + DialtactsActivity.getShowTabIntent(context, ListsFragment.TAB_INDEX_HISTORY); + // TODO (b/35486204): scroll to call + contentIntent.setData(callUri); + return PendingIntent.getActivity(context, 0, contentIntent, PendingIntent.FLAG_UPDATE_CURRENT); } /** Creates a pending intent that marks all new missed calls as old. */ - private PendingIntent createClearMissedCallsPendingIntent() { - Intent intent = new Intent(mContext, CallLogNotificationsService.class); + private PendingIntent createClearMissedCallsPendingIntent(@Nullable Uri callUri) { + Intent intent = new Intent(context, CallLogNotificationsService.class); intent.setAction(CallLogNotificationsService.ACTION_MARK_NEW_MISSED_CALLS_AS_OLD); - return PendingIntent.getService(mContext, 0, intent, 0); + intent.setData(callUri); + return PendingIntent.getService(context, 0, intent, 0); } - private PendingIntent createCallBackPendingIntent(String number) { - Intent intent = new Intent(mContext, CallLogNotificationsService.class); + private PendingIntent createCallBackPendingIntent(String number, @NonNull Uri callUri) { + Intent intent = new Intent(context, CallLogNotificationsService.class); intent.setAction(CallLogNotificationsService.ACTION_CALL_BACK_FROM_MISSED_CALL_NOTIFICATION); intent.putExtra(CallLogNotificationsService.EXTRA_MISSED_CALL_NUMBER, number); + intent.setData(callUri); // Use FLAG_UPDATE_CURRENT to make sure any previous pending intent is updated with the new // extra. - return PendingIntent.getService(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); + return PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); } - private PendingIntent createSendSmsFromNotificationPendingIntent(String number) { - Intent intent = new Intent(mContext, CallLogNotificationsService.class); + private PendingIntent createSendSmsFromNotificationPendingIntent( + String number, @NonNull Uri callUri) { + Intent intent = new Intent(context, CallLogNotificationsService.class); intent.setAction(CallLogNotificationsService.ACTION_SEND_SMS_FROM_MISSED_CALL_NOTIFICATION); intent.putExtra(CallLogNotificationsService.EXTRA_MISSED_CALL_NUMBER, number); + intent.setData(callUri); // Use FLAG_UPDATE_CURRENT to make sure any previous pending intent is updated with the new // extra. - return PendingIntent.getService(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); + return PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); } /** Configures a notification to emit the blinky notification light. */ @@ -325,6 +414,6 @@ public class MissedCallNotifier { } private NotificationManager getNotificationMgr() { - return (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); + return (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); } } diff --git a/java/com/android/dialer/app/calllog/PhoneAccountHandles.java b/java/com/android/dialer/app/calllog/PhoneAccountHandles.java new file mode 100644 index 000000000..6d51b853c --- /dev/null +++ b/java/com/android/dialer/app/calllog/PhoneAccountHandles.java @@ -0,0 +1,41 @@ +package com.android.dialer.app.calllog; + +import android.content.ComponentName; +import android.content.Context; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.telecom.PhoneAccount; +import android.telecom.PhoneAccountHandle; +import com.android.dialer.app.calllog.CallLogNotificationsQueryHelper.NewCall; +import com.android.dialer.common.LogUtil; +import com.android.dialer.telecom.TelecomUtil; + +/** Methods to help extract {@link PhoneAccount} information from database and Telecomm sources. */ +class PhoneAccountHandles { + + @Nullable + public static PhoneAccountHandle getAccount(@NonNull Context context, @Nullable NewCall call) { + PhoneAccountHandle handle; + if (call == null || call.accountComponentName == null || call.accountId == null) { + LogUtil.v( + "PhoneAccountUtils.getAccount", + "accountComponentName == null || callToNotify.accountId == null"); + handle = TelecomUtil.getDefaultOutgoingPhoneAccount(context, PhoneAccount.SCHEME_TEL); + if (handle == null) { + return null; + } + } else { + handle = + new PhoneAccountHandle( + ComponentName.unflattenFromString(call.accountComponentName), call.accountId); + } + if (handle.getComponentName() != null) { + LogUtil.v( + "PhoneAccountUtils.getAccount", + "PhoneAccountHandle.ComponentInfo:" + handle.getComponentName()); + } else { + LogUtil.i("PhoneAccountUtils.getAccount", "PhoneAccountHandle.ComponentInfo: null"); + } + return handle; + } +} diff --git a/java/com/android/dialer/app/calllog/PhoneAccountUtils.java b/java/com/android/dialer/app/calllog/PhoneAccountUtils.java deleted file mode 100644 index c6d94d341..000000000 --- a/java/com/android/dialer/app/calllog/PhoneAccountUtils.java +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Copyright (C) 2013 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.app.calllog; - -import android.content.ComponentName; -import android.content.Context; -import android.support.annotation.Nullable; -import android.telecom.PhoneAccount; -import android.telecom.PhoneAccountHandle; -import android.text.TextUtils; -import com.android.dialer.telecom.TelecomUtil; -import java.util.ArrayList; -import java.util.List; - -/** Methods to help extract {@code PhoneAccount} information from database and Telecomm sources. */ -public class PhoneAccountUtils { - - /** Return a list of phone accounts that are subscription/SIM accounts. */ - public static List getSubscriptionPhoneAccounts(Context context) { - List subscriptionAccountHandles = new ArrayList(); - final List accountHandles = - TelecomUtil.getCallCapablePhoneAccounts(context); - for (PhoneAccountHandle accountHandle : accountHandles) { - PhoneAccount account = TelecomUtil.getPhoneAccount(context, accountHandle); - if (account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) { - subscriptionAccountHandles.add(accountHandle); - } - } - return subscriptionAccountHandles; - } - - /** Compose PhoneAccount object from component name and account id. */ - @Nullable - public static PhoneAccountHandle getAccount( - @Nullable String componentString, @Nullable String accountId) { - if (TextUtils.isEmpty(componentString) || TextUtils.isEmpty(accountId)) { - return null; - } - final ComponentName componentName = ComponentName.unflattenFromString(componentString); - if (componentName == null) { - return null; - } - return new PhoneAccountHandle(componentName, accountId); - } - - /** Extract account label from PhoneAccount object. */ - @Nullable - public static String getAccountLabel( - Context context, @Nullable PhoneAccountHandle accountHandle) { - PhoneAccount account = getAccountOrNull(context, accountHandle); - if (account != null && account.getLabel() != null) { - return account.getLabel().toString(); - } - return null; - } - - /** Extract account color from PhoneAccount object. */ - public static int getAccountColor(Context context, @Nullable PhoneAccountHandle accountHandle) { - final PhoneAccount account = TelecomUtil.getPhoneAccount(context, accountHandle); - - // For single-sim devices the PhoneAccount will be NO_HIGHLIGHT_COLOR by default, so it is - // safe to always use the account highlight color. - return account == null ? PhoneAccount.NO_HIGHLIGHT_COLOR : account.getHighlightColor(); - } - - /** - * Determine whether a phone account supports call subjects. - * - * @return {@code true} if call subjects are supported, {@code false} otherwise. - */ - public static boolean getAccountSupportsCallSubject( - Context context, @Nullable PhoneAccountHandle accountHandle) { - final PhoneAccount account = TelecomUtil.getPhoneAccount(context, accountHandle); - - return account != null && account.hasCapabilities(PhoneAccount.CAPABILITY_CALL_SUBJECT); - } - - /** - * Retrieve the account metadata, but if the account does not exist or the device has only a - * single registered and enabled account, return null. - */ - @Nullable - private static PhoneAccount getAccountOrNull( - Context context, @Nullable PhoneAccountHandle accountHandle) { - if (TelecomUtil.getCallCapablePhoneAccounts(context).size() <= 1) { - return null; - } - return TelecomUtil.getPhoneAccount(context, accountHandle); - } -} diff --git a/java/com/android/dialer/app/calllog/PhoneCallDetailsHelper.java b/java/com/android/dialer/app/calllog/PhoneCallDetailsHelper.java index b18270bb3..acbccb39f 100644 --- a/java/com/android/dialer/app/calllog/PhoneCallDetailsHelper.java +++ b/java/com/android/dialer/app/calllog/PhoneCallDetailsHelper.java @@ -27,9 +27,10 @@ import android.text.TextUtils; import android.text.format.DateUtils; import android.view.View; import android.widget.TextView; -import com.android.dialer.app.PhoneCallDetails; import com.android.dialer.app.R; import com.android.dialer.app.calllog.calllogcache.CallLogCache; +import com.android.dialer.calllogutils.PhoneCallDetails; +import com.android.dialer.oem.MotorolaUtils; import com.android.dialer.phonenumberutil.PhoneNumberHelper; import com.android.dialer.util.DialerUtils; import java.util.ArrayList; @@ -84,6 +85,8 @@ public class PhoneCallDetailsHelper { // Show the video icon if the call had video enabled. views.callTypeIcons.setShowVideo( (details.features & Calls.FEATURES_VIDEO) == Calls.FEATURES_VIDEO); + views.callTypeIcons.setShowHd( + MotorolaUtils.shouldShowHdIconInCallLog(mContext, details.features)); views.callTypeIcons.requestLayout(); views.callTypeIcons.setVisibility(View.VISIBLE); diff --git a/java/com/android/dialer/app/calllog/PhoneCallDetailsViews.java b/java/com/android/dialer/app/calllog/PhoneCallDetailsViews.java index 476996826..e2e27a179 100644 --- a/java/com/android/dialer/app/calllog/PhoneCallDetailsViews.java +++ b/java/com/android/dialer/app/calllog/PhoneCallDetailsViews.java @@ -20,6 +20,7 @@ import android.content.Context; import android.view.View; import android.widget.TextView; import com.android.dialer.app.R; +import com.android.dialer.calllogutils.CallTypeIconsView; /** Encapsulates the views that are used to display the details of a phone call in the call log. */ public final class PhoneCallDetailsViews { diff --git a/java/com/android/dialer/app/calllog/PhoneNumberDisplayUtil.java b/java/com/android/dialer/app/calllog/PhoneNumberDisplayUtil.java deleted file mode 100644 index 410d4cc37..000000000 --- a/java/com/android/dialer/app/calllog/PhoneNumberDisplayUtil.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * Copyright (C) 2011 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.app.calllog; - -import android.content.Context; -import android.provider.CallLog.Calls; -import android.text.BidiFormatter; -import android.text.TextDirectionHeuristics; -import android.text.TextUtils; -import com.android.contacts.common.compat.PhoneNumberUtilsCompat; -import com.android.dialer.app.R; -import com.android.dialer.phonenumberutil.PhoneNumberHelper; - -/** Helper for formatting and managing the display of phone numbers. */ -public class PhoneNumberDisplayUtil { - - /** Returns the string to display for the given phone number if there is no matching contact. */ - /* package */ - static CharSequence getDisplayName( - Context context, CharSequence number, int presentation, boolean isVoicemail) { - if (presentation == Calls.PRESENTATION_UNKNOWN) { - return context.getResources().getString(R.string.unknown); - } - if (presentation == Calls.PRESENTATION_RESTRICTED) { - return PhoneNumberHelper.getDisplayNameForRestrictedNumber(context); - } - if (presentation == Calls.PRESENTATION_PAYPHONE) { - return context.getResources().getString(R.string.payphone); - } - if (isVoicemail) { - return context.getResources().getString(R.string.voicemail); - } - if (PhoneNumberHelper.isLegacyUnknownNumbers(number)) { - return context.getResources().getString(R.string.unknown); - } - return ""; - } - - /** - * Returns the string to display for the given phone number. - * - * @param number the number to display - * @param formattedNumber the formatted number if available, may be null - */ - public static CharSequence getDisplayNumber( - Context context, - CharSequence number, - int presentation, - CharSequence formattedNumber, - CharSequence postDialDigits, - boolean isVoicemail) { - final CharSequence displayName = getDisplayName(context, number, presentation, isVoicemail); - if (!TextUtils.isEmpty(displayName)) { - return getTtsSpannableLtrNumber(displayName); - } - - if (!TextUtils.isEmpty(formattedNumber)) { - return getTtsSpannableLtrNumber(formattedNumber); - } else if (!TextUtils.isEmpty(number)) { - return getTtsSpannableLtrNumber(number.toString() + postDialDigits); - } else { - return context.getResources().getString(R.string.unknown); - } - } - - /** Returns number annotated as phone number in LTR direction. */ - public static CharSequence getTtsSpannableLtrNumber(CharSequence number) { - return PhoneNumberUtilsCompat.createTtsSpannable( - BidiFormatter.getInstance().unicodeWrap(number.toString(), TextDirectionHeuristics.LTR)); - } -} diff --git a/java/com/android/dialer/app/calllog/VisualVoicemailCallLogFragment.java b/java/com/android/dialer/app/calllog/VisualVoicemailCallLogFragment.java index e539ceef6..6f101f580 100644 --- a/java/com/android/dialer/app/calllog/VisualVoicemailCallLogFragment.java +++ b/java/com/android/dialer/app/calllog/VisualVoicemailCallLogFragment.java @@ -40,10 +40,13 @@ public class VisualVoicemailCallLogFragment extends CallLogFragment { private VoicemailErrorManager mVoicemailAlertManager; + public VisualVoicemailCallLogFragment() { + super(CallLog.Calls.VOICEMAIL_TYPE); + } + @Override public void onCreate(Bundle state) { super.onCreate(state); - mCallTypeFilter = CallLog.Calls.VOICEMAIL_TYPE; mVoicemailPlaybackPresenter = VoicemailPlaybackPresenter.getInstance(getActivity(), state); getActivity() .getContentResolver() diff --git a/java/com/android/dialer/app/calllog/VoicemailQueryHandler.java b/java/com/android/dialer/app/calllog/VoicemailQueryHandler.java index d6d8354ec..e73684e70 100644 --- a/java/com/android/dialer/app/calllog/VoicemailQueryHandler.java +++ b/java/com/android/dialer/app/calllog/VoicemailQueryHandler.java @@ -15,13 +15,18 @@ */ package com.android.dialer.app.calllog; +import android.app.NotificationManager; import android.content.AsyncQueryHandler; import android.content.ContentResolver; import android.content.ContentValues; import android.content.Context; import android.content.Intent; +import android.net.Uri; import android.provider.CallLog.Calls; -import android.util.Log; +import android.support.annotation.Nullable; +import com.android.dialer.app.R; +import com.android.dialer.common.LogUtil; +import com.android.dialer.notification.GroupedNotificationUtil; /** Handles asynchronous queries to the call log for voicemail. */ public class VoicemailQueryHandler extends AsyncQueryHandler { @@ -39,7 +44,7 @@ public class VoicemailQueryHandler extends AsyncQueryHandler { } /** Updates all new voicemails to mark them as old. */ - public void markNewVoicemailsAsOld() { + public void markNewVoicemailsAsOld(@Nullable Uri voicemailUri) { // Mark all "new" voicemails as not new anymore. StringBuilder where = new StringBuilder(); where.append(Calls.NEW); @@ -47,6 +52,10 @@ public class VoicemailQueryHandler extends AsyncQueryHandler { where.append(Calls.TYPE); where.append(" = ?"); + if (voicemailUri != null) { + where.append(" AND ").append(Calls.VOICEMAIL_URI).append(" = ?"); + } + ContentValues values = new ContentValues(1); values.put(Calls.NEW, "0"); @@ -56,7 +65,15 @@ public class VoicemailQueryHandler extends AsyncQueryHandler { Calls.CONTENT_URI_WITH_VOICEMAIL, values, where.toString(), - new String[] {Integer.toString(Calls.VOICEMAIL_TYPE)}); + voicemailUri == null + ? new String[] {Integer.toString(Calls.VOICEMAIL_TYPE)} + : new String[] {Integer.toString(Calls.VOICEMAIL_TYPE), voicemailUri.toString()}); + + GroupedNotificationUtil.removeNotification( + mContext.getSystemService(NotificationManager.class), + voicemailUri != null ? voicemailUri.toString() : null, + R.id.notification_voicemail, + DefaultVoicemailNotifier.NOTIFICATION_TAG); } @Override @@ -67,7 +84,7 @@ public class VoicemailQueryHandler extends AsyncQueryHandler { serviceIntent.setAction(CallLogNotificationsService.ACTION_UPDATE_VOICEMAIL_NOTIFICATIONS); mContext.startService(serviceIntent); } else { - Log.w(TAG, "Unknown update completed: ignoring: " + token); + LogUtil.w(TAG, "Unknown update completed: ignoring: " + token); } } } diff --git a/java/com/android/dialer/app/calllog/calllogcache/CallLogCacheLollipopMr1.java b/java/com/android/dialer/app/calllog/calllogcache/CallLogCacheLollipopMr1.java index c342b7e3b..039998780 100644 --- a/java/com/android/dialer/app/calllog/calllogcache/CallLogCacheLollipopMr1.java +++ b/java/com/android/dialer/app/calllog/calllogcache/CallLogCacheLollipopMr1.java @@ -20,10 +20,10 @@ import android.content.Context; import android.support.annotation.VisibleForTesting; import android.telecom.PhoneAccountHandle; import android.text.TextUtils; +import android.util.ArrayMap; import android.util.Pair; -import com.android.dialer.app.calllog.PhoneAccountUtils; +import com.android.dialer.calllogutils.PhoneAccountUtils; import com.android.dialer.phonenumberutil.PhoneNumberHelper; -import java.util.HashMap; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -45,9 +45,9 @@ class CallLogCacheLollipopMr1 extends CallLogCache { final Map, Boolean> mVoicemailQueryCache = new ConcurrentHashMap<>(); - private final Map mPhoneAccountLabelCache = new HashMap<>(); - private final Map mPhoneAccountColorCache = new HashMap<>(); - private final Map mPhoneAccountCallWithNoteCache = new HashMap<>(); + private final Map mPhoneAccountLabelCache = new ArrayMap<>(); + private final Map mPhoneAccountColorCache = new ArrayMap<>(); + private final Map mPhoneAccountCallWithNoteCache = new ArrayMap<>(); /* package */ CallLogCacheLollipopMr1(Context context) { super(context); diff --git a/java/com/android/dialer/app/contactinfo/ContactInfoCache.java b/java/com/android/dialer/app/contactinfo/ContactInfoCache.java index 4135cb7b8..6c35711a8 100644 --- a/java/com/android/dialer/app/contactinfo/ContactInfoCache.java +++ b/java/com/android/dialer/app/contactinfo/ContactInfoCache.java @@ -29,8 +29,8 @@ import java.util.concurrent.BlockingQueue; import java.util.concurrent.PriorityBlockingQueue; /** - * This is a cache of contact details for the phone numbers in the c all log. The key is the phone - * number with the country in which teh call was placed or received. The content of the cache is + * This is a cache of contact details for the phone numbers in the call log. The key is the phone + * number with the country in which the call was placed or received. The content of the cache is * expired (but not purged) whenever the application comes to the foreground. * *

This cache queues request for information and queries for information on a background thread, diff --git a/java/com/android/dialer/app/contactinfo/ContactPhotoLoader.java b/java/com/android/dialer/app/contactinfo/ContactPhotoLoader.java index a8c718502..71e4a16ad 100644 --- a/java/com/android/dialer/app/contactinfo/ContactPhotoLoader.java +++ b/java/com/android/dialer/app/contactinfo/ContactPhotoLoader.java @@ -104,7 +104,7 @@ public class ContactPhotoLoader { final RoundedBitmapDrawable drawable = RoundedBitmapDrawableFactory.create(mContext.getResources(), bitmap); drawable.setAntiAlias(true); - drawable.setCornerRadius(bitmap.getHeight() / 2); + drawable.setCircular(true); return drawable; } catch (IOException e) { LogUtil.e("ContactPhotoLoader.createPhotoIconDrawable", e.toString()); diff --git a/java/com/android/dialer/app/dialpad/DialpadFragment.java b/java/com/android/dialer/app/dialpad/DialpadFragment.java index 18bb250ce..4785ab16f 100644 --- a/java/com/android/dialer/app/dialpad/DialpadFragment.java +++ b/java/com/android/dialer/app/dialpad/DialpadFragment.java @@ -78,9 +78,9 @@ import com.android.dialer.app.DialtactsActivity; import com.android.dialer.app.R; import com.android.dialer.app.SpecialCharSequenceMgr; import com.android.dialer.app.calllog.CallLogAsync; -import com.android.dialer.app.calllog.PhoneAccountUtils; import com.android.dialer.callintent.CallIntentBuilder; import com.android.dialer.callintent.nano.CallInitiationType; +import com.android.dialer.calllogutils.PhoneAccountUtils; import com.android.dialer.common.LogUtil; import com.android.dialer.dialpadview.DialpadKeyButton; import com.android.dialer.dialpadview.DialpadView; @@ -598,6 +598,7 @@ public class DialpadFragment extends Fragment @Override public void onStart() { + LogUtil.d("DialpadFragment.onStart", "first launch: %b", mFirstLaunch); Trace.beginSection(TAG + " onStart"); super.onStart(); // if the mToneGenerator creation fails, just continue without it. It is @@ -624,6 +625,7 @@ public class DialpadFragment extends Fragment @Override public void onResume() { + LogUtil.d("DialpadFragment.onResume", ""); Trace.beginSection(TAG + " onResume"); super.onResume(); diff --git a/java/com/android/dialer/app/filterednumber/BlockedNumbersSettingsActivity.java b/java/com/android/dialer/app/filterednumber/BlockedNumbersSettingsActivity.java index eef920710..9ec6042c0 100644 --- a/java/com/android/dialer/app/filterednumber/BlockedNumbersSettingsActivity.java +++ b/java/com/android/dialer/app/filterednumber/BlockedNumbersSettingsActivity.java @@ -134,11 +134,6 @@ public class BlockedNumbersSettingsActivity extends AppCompatActivity return 0; } - @Override - public int getActionBarHideOffset() { - return 0; - } - @Override public int getActionBarHeight() { return 0; diff --git a/java/com/android/dialer/app/legacybindings/DialerLegacyBindings.java b/java/com/android/dialer/app/legacybindings/DialerLegacyBindings.java index 2125a1524..1cdeb2175 100644 --- a/java/com/android/dialer/app/legacybindings/DialerLegacyBindings.java +++ b/java/com/android/dialer/app/legacybindings/DialerLegacyBindings.java @@ -17,12 +17,14 @@ package com.android.dialer.app.legacybindings; import android.app.Activity; +import android.support.annotation.NonNull; import android.view.ViewGroup; import com.android.dialer.app.calllog.CallLogAdapter; import com.android.dialer.app.calllog.calllogcache.CallLogCache; import com.android.dialer.app.contactinfo.ContactInfoCache; import com.android.dialer.app.list.RegularSearchFragment; import com.android.dialer.app.voicemail.VoicemailPlaybackPresenter; +import com.android.dialer.blocking.FilteredNumberAsyncQueryHandler; /** * These are old bindings between Dialer and the container application. All new bindings should be @@ -41,6 +43,7 @@ public interface DialerLegacyBindings { CallLogCache callLogCache, ContactInfoCache contactInfoCache, VoicemailPlaybackPresenter voicemailPlaybackPresenter, + @NonNull FilteredNumberAsyncQueryHandler filteredNumberAsyncQueryHandler, int activityType); RegularSearchFragment newRegularSearchFragment(); diff --git a/java/com/android/dialer/app/legacybindings/DialerLegacyBindingsStub.java b/java/com/android/dialer/app/legacybindings/DialerLegacyBindingsStub.java index f01df78f8..6e32843ba 100644 --- a/java/com/android/dialer/app/legacybindings/DialerLegacyBindingsStub.java +++ b/java/com/android/dialer/app/legacybindings/DialerLegacyBindingsStub.java @@ -17,12 +17,14 @@ package com.android.dialer.app.legacybindings; import android.app.Activity; +import android.support.annotation.NonNull; import android.view.ViewGroup; import com.android.dialer.app.calllog.CallLogAdapter; import com.android.dialer.app.calllog.calllogcache.CallLogCache; import com.android.dialer.app.contactinfo.ContactInfoCache; import com.android.dialer.app.list.RegularSearchFragment; import com.android.dialer.app.voicemail.VoicemailPlaybackPresenter; +import com.android.dialer.blocking.FilteredNumberAsyncQueryHandler; /** Default implementation for dialer legacy bindings. */ public class DialerLegacyBindingsStub implements DialerLegacyBindings { @@ -35,6 +37,7 @@ public class DialerLegacyBindingsStub implements DialerLegacyBindings { CallLogCache callLogCache, ContactInfoCache contactInfoCache, VoicemailPlaybackPresenter voicemailPlaybackPresenter, + @NonNull FilteredNumberAsyncQueryHandler filteredNumberAsyncQueryHandler, int activityType) { return new CallLogAdapter( activity, @@ -43,6 +46,7 @@ public class DialerLegacyBindingsStub implements DialerLegacyBindings { callLogCache, contactInfoCache, voicemailPlaybackPresenter, + filteredNumberAsyncQueryHandler, activityType); } diff --git a/java/com/android/dialer/app/list/ListsFragment.java b/java/com/android/dialer/app/list/ListsFragment.java index 725ad3001..13938f29a 100644 --- a/java/com/android/dialer/app/list/ListsFragment.java +++ b/java/com/android/dialer/app/list/ListsFragment.java @@ -30,19 +30,16 @@ import android.support.annotation.Nullable; import android.support.v13.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; -import android.support.v7.app.ActionBar; -import android.support.v7.app.AppCompatActivity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.android.contacts.common.list.ViewPagerTabs; import com.android.dialer.app.R; import com.android.dialer.app.calllog.CallLogFragment; -import com.android.dialer.app.calllog.CallLogNotificationsHelper; +import com.android.dialer.app.calllog.CallLogNotificationsService; import com.android.dialer.app.calllog.VisualVoicemailCallLogFragment; import com.android.dialer.app.voicemail.error.VoicemailStatusCorruptionHandler; import com.android.dialer.app.voicemail.error.VoicemailStatusCorruptionHandler.Source; -import com.android.dialer.app.widget.ActionBarController; import com.android.dialer.common.LogUtil; import com.android.dialer.database.CallLogQueryHandler; import com.android.dialer.logging.Logger; @@ -92,7 +89,6 @@ public class ListsFragment extends Fragment public static final int TAB_COUNT_DEFAULT = 3; public static final int TAB_COUNT_WITH_VOICEMAIL = 4; private static final String TAG = "ListsFragment"; - private ActionBar mActionBar; private ViewPager mViewPager; private ViewPagerTabs mViewPagerTabs; private ViewPagerAdapter mViewPagerAdapter; @@ -108,8 +104,7 @@ public class ListsFragment extends Fragment private boolean mHasFetchedVoicemailStatus; private boolean mShowVoicemailTabAfterVoicemailStatusIsFetched; private VoicemailStatusHelper mVoicemailStatusHelper; - private ArrayList mOnPageChangeListeners = - new ArrayList(); + private final ArrayList mOnPageChangeListeners = new ArrayList<>(); private String[] mTabTitles; private int[] mTabIcons; /** The position of the currently selected tab. */ @@ -149,7 +144,6 @@ public class ListsFragment extends Fragment Trace.beginSection(TAG + " onResume"); super.onResume(); - mActionBar = ((AppCompatActivity) getActivity()).getSupportActionBar(); if (getUserVisibleHint()) { sendScreenViewForCurrentPosition(); } @@ -329,7 +323,7 @@ public class ListsFragment extends Fragment .putBoolean( VisualVoicemailEnabledChecker.PREF_KEY_HAS_ACTIVE_VOICEMAIL_PROVIDER, hasActiveVoicemailProvider) - .commit(); + .apply(); } if (hasActiveVoicemailProvider) { @@ -403,7 +397,7 @@ public class ListsFragment extends Fragment public void markMissedCallsAsReadAndRemoveNotifications() { if (mCallLogQueryHandler != null) { mCallLogQueryHandler.markMissedCallsAsRead(); - CallLogNotificationsHelper.removeMissedCallNotifications(getActivity()); + CallLogNotificationsService.markNewMissedCallsAsOld(getContext(), null); } } @@ -413,11 +407,6 @@ public class ListsFragment extends Fragment mRemoveView.animate().alpha(show ? 1 : 0).start(); } - public boolean shouldShowActionBar() { - // TODO: Update this based on scroll state. - return mActionBar != null; - } - public SpeedDialFragment getSpeedDialFragment() { return mSpeedDialFragment; } @@ -486,11 +475,6 @@ public class ListsFragment extends Fragment throw new IllegalStateException("No fragment at position " + position); } - public interface HostInterface { - - ActionBarController getActionBarController(); - } - public class ViewPagerAdapter extends FragmentPagerAdapter { private final List mFragments = new ArrayList<>(); @@ -518,7 +502,7 @@ public class ListsFragment extends Fragment return mSpeedDialFragment; case TAB_INDEX_HISTORY: if (mHistoryFragment == null) { - mHistoryFragment = new CallLogFragment(); + mHistoryFragment = new CallLogFragment(CallLogQueryHandler.CALL_TYPE_ALL); } return mHistoryFragment; case TAB_INDEX_ALL_CONTACTS: diff --git a/java/com/android/dialer/app/list/SearchFragment.java b/java/com/android/dialer/app/list/SearchFragment.java index 4a7d48ae4..e6615aa8d 100644 --- a/java/com/android/dialer/app/list/SearchFragment.java +++ b/java/com/android/dialer/app/list/SearchFragment.java @@ -98,6 +98,7 @@ public class SearchFragment extends PhoneNumberPickerFragment { @Override public void onStart() { + LogUtil.d("SearchFragment.onStart", ""); super.onStart(); if (isSearchMode()) { getAdapter().setHasHeader(0, false); @@ -301,6 +302,7 @@ public class SearchFragment extends PhoneNumberPickerFragment { * shown. This can be optionally animated. */ public void updatePosition(boolean animate) { + LogUtil.d("SearchFragment.updatePosition", "animate: %b", animate); if (mActivity == null) { // Activity will be set in onStart, and this method will be called again return; @@ -363,6 +365,13 @@ public class SearchFragment extends PhoneNumberPickerFragment { return; } int spacerHeight = mActivity.isDialpadShown() ? mActivity.getDialpadHeight() : 0; + LogUtil.d( + "SearchFragment.resizeListView", + "spacerHeight: %d -> %d, isDialpadShown: %b, dialpad height: %d", + mSpacer.getHeight(), + spacerHeight, + mActivity.isDialpadShown(), + mActivity.getDialpadHeight()); if (spacerHeight != mSpacer.getHeight()) { final LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mSpacer.getLayoutParams(); lp.height = spacerHeight; @@ -418,8 +427,6 @@ public class SearchFragment extends PhoneNumberPickerFragment { int getDialpadHeight(); - int getActionBarHideOffset(); - int getActionBarHeight(); } } diff --git a/java/com/android/dialer/app/manifests/activities/AndroidManifest.xml b/java/com/android/dialer/app/manifests/activities/AndroidManifest.xml index 247b34f4c..7e450c4cd 100644 --- a/java/com/android/dialer/app/manifests/activities/AndroidManifest.xml +++ b/java/com/android/dialer/app/manifests/activities/AndroidManifest.xml @@ -29,18 +29,6 @@ android:theme="@style/SettingsStyle"> - - - - - - - - diff --git a/java/com/android/dialer/app/res/drawable-hdpi/ic_call_arrow.png b/java/com/android/dialer/app/res/drawable-hdpi/ic_call_arrow.png deleted file mode 100644 index 14a33e39f..000000000 Binary files a/java/com/android/dialer/app/res/drawable-hdpi/ic_call_arrow.png and /dev/null differ diff --git a/java/com/android/dialer/app/res/drawable-mdpi/ic_call_arrow.png b/java/com/android/dialer/app/res/drawable-mdpi/ic_call_arrow.png deleted file mode 100644 index 169cf2934..000000000 Binary files a/java/com/android/dialer/app/res/drawable-mdpi/ic_call_arrow.png and /dev/null differ diff --git a/java/com/android/dialer/app/res/drawable-xhdpi/ic_call_arrow.png b/java/com/android/dialer/app/res/drawable-xhdpi/ic_call_arrow.png deleted file mode 100644 index 6f1366018..000000000 Binary files a/java/com/android/dialer/app/res/drawable-xhdpi/ic_call_arrow.png and /dev/null differ diff --git a/java/com/android/dialer/app/res/drawable-xxhdpi/ic_call_arrow.png b/java/com/android/dialer/app/res/drawable-xxhdpi/ic_call_arrow.png deleted file mode 100644 index 0364ee015..000000000 Binary files a/java/com/android/dialer/app/res/drawable-xxhdpi/ic_call_arrow.png and /dev/null differ diff --git a/java/com/android/dialer/app/res/drawable-xxxhdpi/ic_call_arrow.png b/java/com/android/dialer/app/res/drawable-xxxhdpi/ic_call_arrow.png deleted file mode 100644 index 8243c2536..000000000 Binary files a/java/com/android/dialer/app/res/drawable-xxxhdpi/ic_call_arrow.png and /dev/null differ diff --git a/java/com/android/dialer/app/res/drawable-xxxhdpi/search_shadow.9.png b/java/com/android/dialer/app/res/drawable-xxxhdpi/search_shadow.9.png new file mode 100644 index 000000000..ff55620d0 Binary files /dev/null and b/java/com/android/dialer/app/res/drawable-xxxhdpi/search_shadow.9.png differ diff --git a/java/com/android/dialer/app/res/layout/call_detail.xml b/java/com/android/dialer/app/res/layout/call_detail.xml deleted file mode 100644 index 58a7bf0dc..000000000 --- a/java/com/android/dialer/app/res/layout/call_detail.xml +++ /dev/null @@ -1,32 +0,0 @@ - - - - - - - - diff --git a/java/com/android/dialer/app/res/layout/call_detail_footer.xml b/java/com/android/dialer/app/res/layout/call_detail_footer.xml deleted file mode 100644 index 57713448e..000000000 --- a/java/com/android/dialer/app/res/layout/call_detail_footer.xml +++ /dev/null @@ -1,52 +0,0 @@ - - - - - - - - - - - - - diff --git a/java/com/android/dialer/app/res/layout/call_detail_header.xml b/java/com/android/dialer/app/res/layout/call_detail_header.xml deleted file mode 100644 index fd85f0af1..000000000 --- a/java/com/android/dialer/app/res/layout/call_detail_header.xml +++ /dev/null @@ -1,89 +0,0 @@ - - - - - - - - - - - - - - - - - - - diff --git a/java/com/android/dialer/app/res/layout/call_detail_history_item.xml b/java/com/android/dialer/app/res/layout/call_detail_history_item.xml index 5958ee81c..0184a42f2 100644 --- a/java/com/android/dialer/app/res/layout/call_detail_history_item.xml +++ b/java/com/android/dialer/app/res/layout/call_detail_history_item.xml @@ -27,9 +27,8 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> - diff --git a/java/com/android/dialer/app/res/layout/call_log_activity.xml b/java/com/android/dialer/app/res/layout/call_log_activity.xml new file mode 100644 index 000000000..4e2b1887c --- /dev/null +++ b/java/com/android/dialer/app/res/layout/call_log_activity.xml @@ -0,0 +1,40 @@ + + + + + + + + diff --git a/java/com/android/dialer/app/res/layout/call_log_list_item.xml b/java/com/android/dialer/app/res/layout/call_log_list_item.xml index c22ac861d..1592aa928 100644 --- a/java/com/android/dialer/app/res/layout/call_log_list_item.xml +++ b/java/com/android/dialer/app/res/layout/call_log_list_item.xml @@ -93,8 +93,7 @@ android:layout_height="wrap_content" android:orientation="horizontal"> - + +

+ + diff --git a/java/com/android/dialer/app/res/menu/dialtacts_options.xml b/java/com/android/dialer/app/res/menu/dialtacts_options.xml index 434aa81d9..25a3e1811 100644 --- a/java/com/android/dialer/app/res/menu/dialtacts_options.xml +++ b/java/com/android/dialer/app/res/menu/dialtacts_options.xml @@ -16,13 +16,17 @@ + android:id="@+id/menu_history" + android:icon="@drawable/ic_menu_history_lt" + android:title="@string/action_menu_call_history_description"/> + diff --git a/java/com/android/dialer/app/res/values/colors.xml b/java/com/android/dialer/app/res/values/colors.xml index b88e55276..cf6b926be 100644 --- a/java/com/android/dialer/app/res/values/colors.xml +++ b/java/com/android/dialer/app/res/values/colors.xml @@ -16,7 +16,6 @@ #ff1744 - #00c853 #fff @color/dialer_theme_color @@ -84,13 +83,6 @@ as call back, play voicemail, etc. --> @color/dialer_theme_color - - #ff2e58 - - @color/dialer_green_highlight_color - - @color/dialer_secondary_text_color - @color/dialer_theme_color_20pct #80ffffff diff --git a/java/com/android/dialer/app/res/values/dimens.xml b/java/com/android/dialer/app/res/values/dimens.xml index f3fd63350..7da29c7a3 100644 --- a/java/com/android/dialer/app/res/values/dimens.xml +++ b/java/com/android/dialer/app/res/values/dimens.xml @@ -28,7 +28,6 @@ 8dp 32dp 54dp - 4dp 13dp 8dp 8dp @@ -68,7 +67,7 @@ 76% 12dp 9dp - 2dp + 1dp 0dp 1dp @@ -143,6 +142,4 @@ 14sp 14sp 48dp - - 12dp diff --git a/java/com/android/dialer/app/res/values/strings.xml b/java/com/android/dialer/app/res/values/strings.xml index 689ee1ba8..66bf70f1a 100644 --- a/java/com/android/dialer/app/res/values/strings.xml +++ b/java/com/android/dialer/app/res/values/strings.xml @@ -55,9 +55,6 @@ Unblock number - - Delete - Edit number before call @@ -94,7 +91,7 @@ Missed calls - %s missed calls + %d missed calls @@ -251,15 +248,13 @@ Settings + + Simulator + All contacts - - Call details - - - Details not available - @@ -275,52 +270,6 @@ is already in progress.) --> Add call - - Incoming call - - - Incoming call transferred to another device - - - Outgoing call - - - Outgoing call transferred to another device - - - Missed call - - - Incoming video call - - - Incoming video call transferred to another device - - - Outgoing video call - - - Outgoing video call transferred to another device - - - Missed video call - - - Voicemail - - - Declined call - - - Blocked call - - - Call answered on another device - Incoming calls @@ -623,28 +572,10 @@ [CHAR LIMIT=NONE] --> Call to ^1, ^2, ^3, ^4. - - on ^1 - - - via %1$s - via %1$s - - on %1$s, via %2$s - @@ -827,6 +758,9 @@ Call blocking + + Voicemail + @@ -955,6 +889,6 @@ Spam - %1$s unavailable right now + %1$s is offline and can\'t be reached diff --git a/java/com/android/dialer/app/res/values/styles.xml b/java/com/android/dialer/app/res/values/styles.xml index ac4422ba2..24521ddaf 100644 --- a/java/com/android/dialer/app/res/values/styles.xml +++ b/java/com/android/dialer/app/res/values/styles.xml @@ -111,11 +111,6 @@ @null - - - - - - \ No newline at end of file diff --git a/java/com/android/dialer/callcomposer/res/values/values.xml b/java/com/android/dialer/callcomposer/res/values/values.xml new file mode 100644 index 000000000..39b8e4071 --- /dev/null +++ b/java/com/android/dialer/callcomposer/res/values/values.xml @@ -0,0 +1,20 @@ + + + + 2 + false + \ No newline at end of file diff --git a/java/com/android/dialer/calldetails/AndroidManifest.xml b/java/com/android/dialer/calldetails/AndroidManifest.xml new file mode 100644 index 000000000..b71207ba2 --- /dev/null +++ b/java/com/android/dialer/calldetails/AndroidManifest.xml @@ -0,0 +1,31 @@ + + + + + + + + + + + + diff --git a/java/com/android/dialer/calldetails/CallDetailsActivity.java b/java/com/android/dialer/calldetails/CallDetailsActivity.java new file mode 100644 index 000000000..6070640a0 --- /dev/null +++ b/java/com/android/dialer/calldetails/CallDetailsActivity.java @@ -0,0 +1,130 @@ +/* + * 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.calldetails; + +import android.content.Context; +import android.content.Intent; +import android.os.AsyncTask; +import android.os.Bundle; +import android.provider.CallLog; +import android.provider.CallLog.Calls; +import android.support.annotation.NonNull; +import android.support.v7.app.AppCompatActivity; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; +import android.view.MenuItem; +import android.widget.Toolbar; +import com.android.dialer.callcomposer.nano.CallComposerContact; +import com.android.dialer.calldetails.nano.CallDetailsEntries; +import com.android.dialer.calldetails.nano.CallDetailsEntries.CallDetailsEntry; +import com.android.dialer.common.Assert; +import com.android.dialer.common.AsyncTaskExecutors; +import com.android.dialer.logging.Logger; +import com.android.dialer.logging.nano.DialerImpression; +import com.android.dialer.protos.ProtoParsers; + +/** Displays the details of a specific call log entry. */ +public class CallDetailsActivity extends AppCompatActivity { + + private static final String EXTRA_CALL_DETAILS_ENTRIES = "call_details_entries"; + private static final String EXTRA_CONTACT = "contact"; + private static final String TASK_DELETE = "task_delete"; + + private CallDetailsEntry[] entries; + + public static Intent newInstance( + Context context, @NonNull CallDetailsEntries details, @NonNull CallComposerContact contact) { + Assert.isNotNull(details); + Assert.isNotNull(contact); + + Intent intent = new Intent(context, CallDetailsActivity.class); + ProtoParsers.put(intent, EXTRA_CONTACT, contact); + ProtoParsers.put(intent, EXTRA_CALL_DETAILS_ENTRIES, details); + return intent; + } + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.call_details_activity); + + Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); + setActionBar(toolbar); + toolbar.inflateMenu(R.menu.call_details_menu); + toolbar.setNavigationOnClickListener(v -> finish()); + onHandleIntent(getIntent()); + } + + @Override + protected void onNewIntent(Intent intent) { + super.onNewIntent(intent); + onHandleIntent(intent); + } + + private void onHandleIntent(Intent intent) { + Bundle arguments = intent.getExtras(); + CallComposerContact contact = + ProtoParsers.getFromInstanceState(arguments, EXTRA_CONTACT, new CallComposerContact()); + entries = + ProtoParsers.getFromInstanceState( + arguments, EXTRA_CALL_DETAILS_ENTRIES, new CallDetailsEntries()) + .entries; + RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view); + recyclerView.setLayoutManager(new LinearLayoutManager(this)); + recyclerView.setAdapter(new CallDetailsAdapter(this, contact, entries)); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + if (item.getItemId() == R.id.call_detail_delete_menu_item) { + Logger.get(this).logImpression(DialerImpression.Type.USER_DELETED_CALL_LOG_ITEM); + AsyncTaskExecutors.createAsyncTaskExecutor().submit(TASK_DELETE, new DeleteCallsTask()); + item.setEnabled(false); + return true; + } + return super.onOptionsItemSelected(item); + } + + /** Delete specified calls from the call log. */ + private class DeleteCallsTask extends AsyncTask { + + private final String callIds; + + DeleteCallsTask() { + StringBuilder callIds = new StringBuilder(); + for (CallDetailsEntry entry : entries) { + if (callIds.length() != 0) { + callIds.append(","); + } + callIds.append(entry.callId); + } + this.callIds = callIds.toString(); + } + + @Override + protected Void doInBackground(Void... params) { + getContentResolver() + .delete(Calls.CONTENT_URI, CallLog.Calls._ID + " IN (" + callIds + ")", null); + return null; + } + + @Override + public void onPostExecute(Void result) { + finish(); + } + } +} diff --git a/java/com/android/dialer/calldetails/CallDetailsAdapter.java b/java/com/android/dialer/calldetails/CallDetailsAdapter.java new file mode 100644 index 000000000..954583077 --- /dev/null +++ b/java/com/android/dialer/calldetails/CallDetailsAdapter.java @@ -0,0 +1,97 @@ +/* + * 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.calldetails; + +import android.content.Context; +import android.support.v7.widget.RecyclerView; +import android.support.v7.widget.RecyclerView.ViewHolder; +import android.view.LayoutInflater; +import android.view.ViewGroup; +import com.android.dialer.callcomposer.nano.CallComposerContact; +import com.android.dialer.calldetails.nano.CallDetailsEntries.CallDetailsEntry; +import com.android.dialer.calllogutils.CallTypeHelper; +import com.android.dialer.common.Assert; + +/** Adapter for RecyclerView in {@link CallDetailsActivity}. */ +public class CallDetailsAdapter extends RecyclerView.Adapter { + + private static final int HEADER_VIEW_TYPE = 1; + private static final int CALL_ENTRY_VIEW_TYPE = 2; + private static final int FOOTER_VIEW_TYPE = 3; + + private final CallComposerContact contact; + private final CallDetailsEntry[] callDetailsEntries; + private final CallTypeHelper callTypeHelper; + + public CallDetailsAdapter( + Context context, CallComposerContact contact, CallDetailsEntry[] callDetailsEntries) { + this.contact = Assert.isNotNull(contact); + this.callDetailsEntries = Assert.isNotNull(callDetailsEntries); + callTypeHelper = new CallTypeHelper(context.getResources()); + } + + @Override + public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { + LayoutInflater inflater = LayoutInflater.from(parent.getContext()); + switch (viewType) { + case HEADER_VIEW_TYPE: + return new CallDetailsHeaderViewHolder( + inflater.inflate(R.layout.contact_container, parent, false)); + case CALL_ENTRY_VIEW_TYPE: + return new CallDetailsEntryViewHolder( + inflater.inflate(R.layout.call_details_entry, parent, false)); + case FOOTER_VIEW_TYPE: + return new CallDetailsFooterViewHolder( + inflater.inflate(R.layout.call_details_footer, parent, false)); + default: + Assert.fail("No ViewHolder available for viewType: " + viewType); + return null; + } + } + + @Override + public void onBindViewHolder(ViewHolder holder, int position) { + if (position == 0) { // Header + ((CallDetailsHeaderViewHolder) holder).updateContactInfo(contact); + } else if (position == getItemCount() - 1) { + ((CallDetailsFooterViewHolder) holder).setPhoneNumber(contact.number); + } else { + CallDetailsEntryViewHolder viewHolder = (CallDetailsEntryViewHolder) holder; + viewHolder.setCallDetails( + contact.number, + callDetailsEntries[position - 1], + callTypeHelper, + position != getItemCount() - 2); + } + } + + @Override + public int getItemViewType(int position) { + if (position == 0) { // Header + return HEADER_VIEW_TYPE; + } else if (position == getItemCount() - 1) { + return FOOTER_VIEW_TYPE; + } else { + return CALL_ENTRY_VIEW_TYPE; + } + } + + @Override + public int getItemCount() { + return callDetailsEntries.length + 2; // Header + footer + } +} diff --git a/java/com/android/dialer/calldetails/CallDetailsEntryViewHolder.java b/java/com/android/dialer/calldetails/CallDetailsEntryViewHolder.java new file mode 100644 index 000000000..b1a70af0c --- /dev/null +++ b/java/com/android/dialer/calldetails/CallDetailsEntryViewHolder.java @@ -0,0 +1,195 @@ +/* + * 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.calldetails; + +import android.content.Context; +import android.content.res.Resources; +import android.graphics.PorterDuff; +import android.graphics.drawable.Drawable; +import android.net.Uri; +import android.provider.CallLog.Calls; +import android.support.annotation.ColorInt; +import android.support.annotation.NonNull; +import android.support.v4.content.ContextCompat; +import android.support.v7.widget.RecyclerView.ViewHolder; +import android.text.TextUtils; +import android.view.View; +import android.widget.ImageView; +import android.widget.TextView; +import com.android.dialer.calldetails.nano.CallDetailsEntries.CallDetailsEntry; +import com.android.dialer.calllogutils.CallEntryFormatter; +import com.android.dialer.calllogutils.CallTypeHelper; +import com.android.dialer.common.LogUtil; +import com.android.dialer.compat.AppCompatConstants; +import com.android.dialer.enrichedcall.historyquery.proto.nano.HistoryResult; +import com.android.dialer.enrichedcall.historyquery.proto.nano.HistoryResult.Type; +import com.android.dialer.util.CallUtil; +import com.android.dialer.util.DialerUtils; +import com.android.dialer.util.IntentUtil; + +/** ViewHolder for call entries in {@link CallDetailsActivity}. */ +public class CallDetailsEntryViewHolder extends ViewHolder { + + private final ImageView callTypeIcon; + private final TextView callTypeText; + private final TextView callTime; + private final TextView callDuration; + + private final View multimediaImageContainer; + private final View multimediaDetailsContainer; + private final View multimediaDivider; + + private final TextView multimediaDetails; + + private final ImageView multimediaImage; + + // TODO: Display this when location is stored - b/36160042 + @SuppressWarnings("unused") + private final TextView multimediaAttachmentsNumber; + + private final Context context; + + public CallDetailsEntryViewHolder(View container) { + super(container); + context = container.getContext(); + + callTypeIcon = (ImageView) container.findViewById(R.id.call_direction); + callTypeText = (TextView) container.findViewById(R.id.call_type); + callTime = (TextView) container.findViewById(R.id.call_time); + callDuration = (TextView) container.findViewById(R.id.call_duration); + + multimediaImageContainer = container.findViewById(R.id.multimedia_image_container); + multimediaDetailsContainer = container.findViewById(R.id.ec_container); + multimediaDivider = container.findViewById(R.id.divider); + multimediaDetails = (TextView) container.findViewById(R.id.multimedia_details); + multimediaImage = (ImageView) container.findViewById(R.id.multimedia_image); + multimediaAttachmentsNumber = + (TextView) container.findViewById(R.id.multimedia_attachments_number); + } + + void setCallDetails( + String number, + CallDetailsEntry entry, + CallTypeHelper callTypeHelper, + boolean showMultimediaDivider) { + int callType = entry.callType; + boolean isVideoCall = + (entry.features & Calls.FEATURES_VIDEO) == Calls.FEATURES_VIDEO + && CallUtil.isVideoEnabled(context); + boolean isPulledCall = + (entry.features & Calls.FEATURES_PULLED_EXTERNALLY) == Calls.FEATURES_PULLED_EXTERNALLY; + + Drawable callIcon = getIconForCallType(context.getResources(), callType); + int color = getColorForCallType(context, callType); + callIcon.setColorFilter(color, PorterDuff.Mode.MULTIPLY); + callTime.setTextColor(color); + callTypeIcon.setImageDrawable(callIcon); + + callTypeText.setText(callTypeHelper.getCallTypeText(callType, isVideoCall, isPulledCall)); + callTime.setText(CallEntryFormatter.formatDate(context, entry.date)); + if (CallTypeHelper.isMissedCallType(callType)) { + callDuration.setVisibility(View.GONE); + } else { + callDuration.setVisibility(View.VISIBLE); + callDuration.setText( + CallEntryFormatter.formatDurationAndDataUsage(context, entry.duration, entry.dataUsage)); + } + setMultimediaDetails(number, entry, showMultimediaDivider); + } + + private void setMultimediaDetails(String number, CallDetailsEntry entry, boolean showDivider) { + multimediaDivider.setVisibility(showDivider ? View.VISIBLE : View.GONE); + if (entry.historyResults == null || entry.historyResults.length <= 0) { + LogUtil.i("CallDetailsEntryViewHolder.setMultimediaDetails", "no data, hiding UI"); + multimediaDetailsContainer.setVisibility(View.GONE); + } else { + + // TODO: b/36158891 Add room for 2 pieces of enriched call data. It's possible + // to have both call composer data and post call data for a single call. + HistoryResult historyResult = entry.historyResults[0]; + multimediaDetailsContainer.setVisibility(View.VISIBLE); + multimediaDetailsContainer.setOnClickListener( + (v) -> { + DialerUtils.startActivityWithErrorToast(context, IntentUtil.getSendSmsIntent(number)); + }); + multimediaImageContainer.setClipToOutline(true); + + if (!TextUtils.isEmpty(historyResult.imageUri)) { + LogUtil.i("CallDetailsEntryViewHolder.setMultimediaDetails", "setting image"); + multimediaImageContainer.setVisibility(View.VISIBLE); + multimediaImage.setImageURI(Uri.parse(historyResult.imageUri)); + multimediaDetails.setText( + isIncoming(historyResult) ? R.string.received_a_photo : R.string.sent_a_photo); + } else { + LogUtil.i("CallDetailsEntryViewHolder.setMultimediaDetails", "no image"); + } + + // Set text after image to overwrite the received/sent a photo text + if (!TextUtils.isEmpty(historyResult.text)) { + LogUtil.i("CallDetailsEntryViewHolder.setMultimediaDetails", "showing text"); + multimediaDetails.setText( + context.getString(R.string.message_in_quotes, historyResult.text)); + } else { + LogUtil.i("CallDetailsEntryViewHolder.setMultimediaDetails", "no text"); + } + } + } + + private static boolean isIncoming(@NonNull HistoryResult historyResult) { + return historyResult.type == Type.INCOMING_POST_CALL + || historyResult.type == Type.INCOMING_CALL_COMPOSER; + } + + private static Drawable getIconForCallType(Resources resources, int callType) { + switch (callType) { + case AppCompatConstants.CALLS_OUTGOING_TYPE: + return resources.getDrawable(R.drawable.quantum_ic_call_made_white_24); + case AppCompatConstants.CALLS_BLOCKED_TYPE: + return resources.getDrawable(R.drawable.quantum_ic_block_white_24); + case AppCompatConstants.CALLS_INCOMING_TYPE: + case AppCompatConstants.CALLS_ANSWERED_EXTERNALLY_TYPE: + case AppCompatConstants.CALLS_REJECTED_TYPE: + return resources.getDrawable(R.drawable.quantum_ic_call_received_white_24); + case AppCompatConstants.CALLS_MISSED_TYPE: + default: + // It is possible for users to end up with calls with unknown call types in their + // call history, possibly due to 3rd party call log implementations (e.g. to + // distinguish between rejected and missed calls). Instead of crashing, just + // assume that all unknown call types are missed calls. + return resources.getDrawable(R.drawable.quantum_ic_call_missed_white_24); + } + } + + private static @ColorInt int getColorForCallType(Context context, int callType) { + switch (callType) { + case AppCompatConstants.CALLS_OUTGOING_TYPE: + case AppCompatConstants.CALLS_VOICEMAIL_TYPE: + case AppCompatConstants.CALLS_BLOCKED_TYPE: + case AppCompatConstants.CALLS_INCOMING_TYPE: + case AppCompatConstants.CALLS_ANSWERED_EXTERNALLY_TYPE: + case AppCompatConstants.CALLS_REJECTED_TYPE: + return ContextCompat.getColor(context, R.color.dialer_secondary_text_color); + case AppCompatConstants.CALLS_MISSED_TYPE: + default: + // It is possible for users to end up with calls with unknown call types in their + // call history, possibly due to 3rd party call log implementations (e.g. to + // distinguish between rejected and missed calls). Instead of crashing, just + // assume that all unknown call types are missed calls. + return ContextCompat.getColor(context, R.color.missed_call); + } + } +} diff --git a/java/com/android/dialer/calldetails/CallDetailsFooterViewHolder.java b/java/com/android/dialer/calldetails/CallDetailsFooterViewHolder.java new file mode 100644 index 000000000..36662bab9 --- /dev/null +++ b/java/com/android/dialer/calldetails/CallDetailsFooterViewHolder.java @@ -0,0 +1,67 @@ +/* + * 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.calldetails; + +import android.content.Context; +import android.content.Intent; +import android.support.v7.widget.RecyclerView; +import android.view.View; +import android.view.View.OnClickListener; +import com.android.contacts.common.ClipboardUtils; +import com.android.dialer.common.Assert; +import com.android.dialer.logging.Logger; +import com.android.dialer.logging.nano.DialerImpression; +import com.android.dialer.util.CallUtil; +import com.android.dialer.util.DialerUtils; + +/** ViewHolder container for {@link CallDetailsActivity} footer. */ +public class CallDetailsFooterViewHolder extends RecyclerView.ViewHolder + implements OnClickListener { + + private final View copy; + private final View edit; + + private String number; + + public CallDetailsFooterViewHolder(View view) { + super(view); + copy = view.findViewById(R.id.call_detail_action_copy); + edit = view.findViewById(R.id.call_detail_action_edit_before_call); + + copy.setOnClickListener(this); + edit.setOnClickListener(this); + } + + public void setPhoneNumber(String number) { + this.number = number; + } + + @Override + public void onClick(View view) { + Context context = view.getContext(); + if (view == copy) { + Logger.get(context).logImpression(DialerImpression.Type.CALL_DETAILS_COPY_NUMBER); + ClipboardUtils.copyText(context, null, number, true); + } else if (view == edit) { + Logger.get(context).logImpression(DialerImpression.Type.CALL_DETAILS_EDIT_BEFORE_CALL); + Intent dialIntent = new Intent(Intent.ACTION_DIAL, CallUtil.getCallUri(number)); + DialerUtils.startActivityWithErrorToast(context, dialIntent); + } else { + Assert.fail("View on click not implemented: " + view); + } + } +} diff --git a/java/com/android/dialer/calldetails/CallDetailsHeaderViewHolder.java b/java/com/android/dialer/calldetails/CallDetailsHeaderViewHolder.java new file mode 100644 index 000000000..1679c2baf --- /dev/null +++ b/java/com/android/dialer/calldetails/CallDetailsHeaderViewHolder.java @@ -0,0 +1,125 @@ +/* + * 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.calldetails; + +import android.content.Context; +import android.net.Uri; +import android.support.v7.widget.RecyclerView; +import android.text.TextUtils; +import android.view.View; +import android.view.View.OnClickListener; +import android.widget.QuickContactBadge; +import android.widget.TextView; +import com.android.contacts.common.ContactPhotoManager; +import com.android.contacts.common.util.UriUtils; +import com.android.dialer.callcomposer.nano.CallComposerContact; +import com.android.dialer.callintent.CallIntentBuilder; +import com.android.dialer.callintent.nano.CallInitiationType; +import com.android.dialer.common.Assert; +import com.android.dialer.logging.Logger; +import com.android.dialer.logging.nano.DialerImpression; +import com.android.dialer.util.DialerUtils; + +/** ViewHolder for Header/Contact in {@link CallDetailsActivity}. */ +public class CallDetailsHeaderViewHolder extends RecyclerView.ViewHolder + implements OnClickListener { + + private final View callBackButton; + private final TextView nameView; + private final TextView numberView; + private final QuickContactBadge contactPhoto; + private final Context context; + + private CallComposerContact contact; + + CallDetailsHeaderViewHolder(View container) { + super(container); + context = container.getContext(); + callBackButton = container.findViewById(R.id.call_back_button); + nameView = (TextView) container.findViewById(R.id.contact_name); + numberView = (TextView) container.findViewById(R.id.phone_number); + contactPhoto = (QuickContactBadge) container.findViewById(R.id.quick_contact_photo); + callBackButton.setOnClickListener(this); + } + + /** + * Populates the contact info fields based on the current contact information. Copied from {@link + * com.android.contacts.common.dialog.CallSubjectDialog}. + */ + public void updateContactInfo(CallComposerContact contact) { + this.contact = contact; + setPhoto( + contact.photoId, + Uri.parse(contact.photoUri), + Uri.parse(contact.contactUri), + contact.nameOrNumber, + contact.isBusiness); + + nameView.setText(contact.nameOrNumber); + if (!TextUtils.isEmpty(contact.numberLabel) && !TextUtils.isEmpty(contact.displayNumber)) { + numberView.setVisibility(View.VISIBLE); + String secondaryInfo = + context.getString( + com.android.contacts.common.R.string.call_subject_type_and_number, + contact.numberLabel, + contact.displayNumber); + numberView.setText(secondaryInfo); + } else { + numberView.setVisibility(View.GONE); + numberView.setText(null); + } + } + + /** + * Sets the photo on the quick contact galleryIcon. Copied from {@link + * com.android.contacts.common.dialog.CallSubjectDialog}. + */ + private void setPhoto( + long photoId, Uri photoUri, Uri contactUri, String displayName, boolean isBusiness) { + contactPhoto.assignContactUri(contactUri); + contactPhoto.setOverlay(null); + + int contactType = + isBusiness ? ContactPhotoManager.TYPE_BUSINESS : ContactPhotoManager.TYPE_DEFAULT; + String lookupKey = contactUri == null ? null : UriUtils.getLookupKeyFromUri(contactUri); + + ContactPhotoManager.DefaultImageRequest request = + new ContactPhotoManager.DefaultImageRequest( + displayName, lookupKey, contactType, true /* isCircular */); + + if (photoId == 0 && photoUri != null) { + contactPhoto.setImageDrawable( + context.getDrawable(R.drawable.product_logo_avatar_anonymous_color_120)); + } else { + ContactPhotoManager.getInstance(context) + .loadThumbnail( + contactPhoto, photoId, false /* darkTheme */, true /* isCircular */, request); + } + } + + @Override + public void onClick(View view) { + if (view == callBackButton) { + Logger.get(view.getContext()).logImpression(DialerImpression.Type.CALL_DETAILS_CALL_BACK); + DialerUtils.startActivityWithErrorToast( + view.getContext(), + new CallIntentBuilder(contact.number, CallInitiationType.Type.CALL_DETAILS).build()); + } else { + Assert.fail("View OnClickListener not implemented: " + view); + } + } +} diff --git a/java/com/android/dialer/calldetails/nano/CallDetailsEntries.java b/java/com/android/dialer/calldetails/nano/CallDetailsEntries.java new file mode 100644 index 000000000..aee8f3652 --- /dev/null +++ b/java/com/android/dialer/calldetails/nano/CallDetailsEntries.java @@ -0,0 +1,440 @@ +/* + * 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. + */ + +// Generated by the protocol buffer compiler. DO NOT EDIT! + +package com.android.dialer.calldetails.nano; + +/** This file is autogenerated, but javadoc required. */ +@SuppressWarnings("hiding") +public final class CallDetailsEntries + extends com.google.protobuf.nano.ExtendableMessageNano { + + /** This file is autogenerated, but javadoc required. */ + public static final class CallDetailsEntry + extends com.google.protobuf.nano.ExtendableMessageNano { + + private static volatile CallDetailsEntry[] _emptyArray; + public static CallDetailsEntry[] emptyArray() { + // Lazily initializes the empty array + if (_emptyArray == null) { + synchronized (com.google.protobuf.nano.InternalNano.LAZY_INIT_LOCK) { + if (_emptyArray == null) { + _emptyArray = new CallDetailsEntry[0]; + } + } + } + return _emptyArray; + } + + // optional int64 call_id = 1; + public long callId; + + // optional int32 call_type = 2; + public int callType; + + // optional int32 features = 3; + public int features; + + // optional int64 date = 4; + public long date; + + // optional int64 duration = 5; + public long duration; + + // optional int64 data_usage = 6; + public long dataUsage; + + // repeated .com.android.dialer.enrichedcall.historyquery.proto. + // HistoryResult history_results = 7; + public com.android.dialer.enrichedcall.historyquery.proto.nano.HistoryResult[] historyResults; + + // @@protoc_insertion_point(class_scope:com.android.dialer.calldetails.CallDetailsEntries.CallDetailsEntry) + + public CallDetailsEntry() { + clear(); + } + + public CallDetailsEntry clear() { + callId = 0L; + callType = 0; + features = 0; + date = 0L; + duration = 0L; + dataUsage = 0L; + historyResults = + com.android.dialer.enrichedcall.historyquery.proto.nano.HistoryResult.emptyArray(); + unknownFieldData = null; + cachedSize = -1; + return this; + } + + @Override + public boolean equals(Object o) { + if (o == this) { + return true; + } + if (!(o instanceof CallDetailsEntry)) { + return false; + } + CallDetailsEntry other = (CallDetailsEntry) o; + if (this.callId != other.callId) { + return false; + } + if (this.callType != other.callType) { + return false; + } + if (this.features != other.features) { + return false; + } + if (this.date != other.date) { + return false; + } + if (this.duration != other.duration) { + return false; + } + if (this.dataUsage != other.dataUsage) { + return false; + } + if (!com.google.protobuf.nano.InternalNano.equals( + this.historyResults, other.historyResults)) { + return false; + } + if (unknownFieldData == null || unknownFieldData.isEmpty()) { + return other.unknownFieldData == null || other.unknownFieldData.isEmpty(); + } else { + return unknownFieldData.equals(other.unknownFieldData); + } + } + + @Override + public int hashCode() { + int result = 17; + result = 31 * result + getClass().getName().hashCode(); + result = 31 * result + (int) (this.callId ^ (this.callId >>> 32)); + result = 31 * result + this.callType; + result = 31 * result + this.features; + result = 31 * result + (int) (this.date ^ (this.date >>> 32)); + result = 31 * result + (int) (this.duration ^ (this.duration >>> 32)); + result = 31 * result + (int) (this.dataUsage ^ (this.dataUsage >>> 32)); + result = 31 * result + com.google.protobuf.nano.InternalNano.hashCode(this.historyResults); + result = + 31 * result + + (unknownFieldData == null || unknownFieldData.isEmpty() + ? 0 + : unknownFieldData.hashCode()); + return result; + } + + @Override + public void writeTo(com.google.protobuf.nano.CodedOutputByteBufferNano output) + throws java.io.IOException { + if (this.callId != 0L) { + output.writeInt64(1, this.callId); + } + if (this.callType != 0) { + output.writeInt32(2, this.callType); + } + if (this.features != 0) { + output.writeInt32(3, this.features); + } + if (this.date != 0L) { + output.writeInt64(4, this.date); + } + if (this.duration != 0L) { + output.writeInt64(5, this.duration); + } + if (this.dataUsage != 0L) { + output.writeInt64(6, this.dataUsage); + } + if (this.historyResults != null && this.historyResults.length > 0) { + for (int i = 0; i < this.historyResults.length; i++) { + com.android.dialer.enrichedcall.historyquery.proto.nano.HistoryResult element = + this.historyResults[i]; + if (element != null) { + output.writeMessage(7, element); + } + } + } + super.writeTo(output); + } + + @Override + protected int computeSerializedSize() { + int size = super.computeSerializedSize(); + if (this.callId != 0L) { + size += com.google.protobuf.nano.CodedOutputByteBufferNano.computeInt64Size(1, this.callId); + } + if (this.callType != 0) { + size += + com.google.protobuf.nano.CodedOutputByteBufferNano.computeInt32Size(2, this.callType); + } + if (this.features != 0) { + size += + com.google.protobuf.nano.CodedOutputByteBufferNano.computeInt32Size(3, this.features); + } + if (this.date != 0L) { + size += com.google.protobuf.nano.CodedOutputByteBufferNano.computeInt64Size(4, this.date); + } + if (this.duration != 0L) { + size += + com.google.protobuf.nano.CodedOutputByteBufferNano.computeInt64Size(5, this.duration); + } + if (this.dataUsage != 0L) { + size += + com.google.protobuf.nano.CodedOutputByteBufferNano.computeInt64Size(6, this.dataUsage); + } + if (this.historyResults != null && this.historyResults.length > 0) { + for (int i = 0; i < this.historyResults.length; i++) { + com.android.dialer.enrichedcall.historyquery.proto.nano.HistoryResult element = + this.historyResults[i]; + if (element != null) { + size += + com.google.protobuf.nano.CodedOutputByteBufferNano.computeMessageSize(7, element); + } + } + } + return size; + } + + @Override + public CallDetailsEntry mergeFrom(com.google.protobuf.nano.CodedInputByteBufferNano input) + throws java.io.IOException { + while (true) { + int tag = input.readTag(); + switch (tag) { + case 0: + return this; + default: + { + if (!super.storeUnknownField(input, tag)) { + return this; + } + break; + } + case 8: + { + this.callId = input.readInt64(); + break; + } + case 16: + { + this.callType = input.readInt32(); + break; + } + case 24: + { + this.features = input.readInt32(); + break; + } + case 32: + { + this.date = input.readInt64(); + break; + } + case 40: + { + this.duration = input.readInt64(); + break; + } + case 48: + { + this.dataUsage = input.readInt64(); + break; + } + case 58: + { + int arrayLength = + com.google.protobuf.nano.WireFormatNano.getRepeatedFieldArrayLength(input, 58); + int i = this.historyResults == null ? 0 : this.historyResults.length; + com.android.dialer.enrichedcall.historyquery.proto.nano.HistoryResult[] newArray = + new com.android.dialer.enrichedcall.historyquery.proto.nano.HistoryResult + [i + arrayLength]; + if (i != 0) { + java.lang.System.arraycopy(this.historyResults, 0, newArray, 0, i); + } + for (; i < newArray.length - 1; i++) { + newArray[i] = + new com.android.dialer.enrichedcall.historyquery.proto.nano.HistoryResult(); + input.readMessage(newArray[i]); + input.readTag(); + } + // Last one without readTag. + newArray[i] = + new com.android.dialer.enrichedcall.historyquery.proto.nano.HistoryResult(); + input.readMessage(newArray[i]); + this.historyResults = newArray; + break; + } + } + } + } + + public static CallDetailsEntry parseFrom(byte[] data) + throws com.google.protobuf.nano.InvalidProtocolBufferNanoException { + return com.google.protobuf.nano.MessageNano.mergeFrom(new CallDetailsEntry(), data); + } + + public static CallDetailsEntry parseFrom( + com.google.protobuf.nano.CodedInputByteBufferNano input) throws java.io.IOException { + return new CallDetailsEntry().mergeFrom(input); + } + } + + private static volatile CallDetailsEntries[] _emptyArray; + public static CallDetailsEntries[] emptyArray() { + // Lazily initializes the empty array + if (_emptyArray == null) { + synchronized (com.google.protobuf.nano.InternalNano.LAZY_INIT_LOCK) { + if (_emptyArray == null) { + _emptyArray = new CallDetailsEntries[0]; + } + } + } + return _emptyArray; + } + + // repeated .com.android.dialer.calldetails.CallDetailsEntries.CallDetailsEntry entries = 1; + public com.android.dialer.calldetails.nano.CallDetailsEntries.CallDetailsEntry[] entries; + + // @@protoc_insertion_point(class_scope:com.android.dialer.calldetails.CallDetailsEntries) + + public CallDetailsEntries() { + clear(); + } + + public CallDetailsEntries clear() { + entries = com.android.dialer.calldetails.nano.CallDetailsEntries.CallDetailsEntry.emptyArray(); + unknownFieldData = null; + cachedSize = -1; + return this; + } + + @Override + public boolean equals(Object o) { + if (o == this) { + return true; + } + if (!(o instanceof CallDetailsEntries)) { + return false; + } + CallDetailsEntries other = (CallDetailsEntries) o; + if (!com.google.protobuf.nano.InternalNano.equals(this.entries, other.entries)) { + return false; + } + if (unknownFieldData == null || unknownFieldData.isEmpty()) { + return other.unknownFieldData == null || other.unknownFieldData.isEmpty(); + } else { + return unknownFieldData.equals(other.unknownFieldData); + } + } + + @Override + public int hashCode() { + int result = 17; + result = 31 * result + getClass().getName().hashCode(); + result = 31 * result + com.google.protobuf.nano.InternalNano.hashCode(this.entries); + result = + 31 * result + + (unknownFieldData == null || unknownFieldData.isEmpty() + ? 0 + : unknownFieldData.hashCode()); + return result; + } + + @Override + public void writeTo(com.google.protobuf.nano.CodedOutputByteBufferNano output) + throws java.io.IOException { + if (this.entries != null && this.entries.length > 0) { + for (int i = 0; i < this.entries.length; i++) { + com.android.dialer.calldetails.nano.CallDetailsEntries.CallDetailsEntry element = + this.entries[i]; + if (element != null) { + output.writeMessage(1, element); + } + } + } + super.writeTo(output); + } + + @Override + protected int computeSerializedSize() { + int size = super.computeSerializedSize(); + if (this.entries != null && this.entries.length > 0) { + for (int i = 0; i < this.entries.length; i++) { + com.android.dialer.calldetails.nano.CallDetailsEntries.CallDetailsEntry element = + this.entries[i]; + if (element != null) { + size += com.google.protobuf.nano.CodedOutputByteBufferNano.computeMessageSize(1, element); + } + } + } + return size; + } + + @Override + public CallDetailsEntries mergeFrom(com.google.protobuf.nano.CodedInputByteBufferNano input) + throws java.io.IOException { + while (true) { + int tag = input.readTag(); + switch (tag) { + case 0: + return this; + default: + { + if (!super.storeUnknownField(input, tag)) { + return this; + } + break; + } + case 10: + { + int arrayLength = + com.google.protobuf.nano.WireFormatNano.getRepeatedFieldArrayLength(input, 10); + int i = this.entries == null ? 0 : this.entries.length; + com.android.dialer.calldetails.nano.CallDetailsEntries.CallDetailsEntry[] newArray = + new com.android.dialer.calldetails.nano.CallDetailsEntries.CallDetailsEntry + [i + arrayLength]; + if (i != 0) { + java.lang.System.arraycopy(this.entries, 0, newArray, 0, i); + } + for (; i < newArray.length - 1; i++) { + newArray[i] = + new com.android.dialer.calldetails.nano.CallDetailsEntries.CallDetailsEntry(); + input.readMessage(newArray[i]); + input.readTag(); + } + // Last one without readTag. + newArray[i] = + new com.android.dialer.calldetails.nano.CallDetailsEntries.CallDetailsEntry(); + input.readMessage(newArray[i]); + this.entries = newArray; + break; + } + } + } + } + + public static CallDetailsEntries parseFrom(byte[] data) + throws com.google.protobuf.nano.InvalidProtocolBufferNanoException { + return com.google.protobuf.nano.MessageNano.mergeFrom(new CallDetailsEntries(), data); + } + + public static CallDetailsEntries parseFrom( + com.google.protobuf.nano.CodedInputByteBufferNano input) throws java.io.IOException { + return new CallDetailsEntries().mergeFrom(input); + } +} diff --git a/java/com/android/dialer/calldetails/res/drawable/multimedia_image_background.xml b/java/com/android/dialer/calldetails/res/drawable/multimedia_image_background.xml new file mode 100644 index 000000000..421bdbfee --- /dev/null +++ b/java/com/android/dialer/calldetails/res/drawable/multimedia_image_background.xml @@ -0,0 +1,20 @@ + + + + + diff --git a/java/com/android/dialer/calldetails/res/layout/call_details_activity.xml b/java/com/android/dialer/calldetails/res/layout/call_details_activity.xml new file mode 100644 index 000000000..038a8745e --- /dev/null +++ b/java/com/android/dialer/calldetails/res/layout/call_details_activity.xml @@ -0,0 +1,37 @@ + + + + + + + + \ No newline at end of file diff --git a/java/com/android/dialer/calldetails/res/layout/call_details_entry.xml b/java/com/android/dialer/calldetails/res/layout/call_details_entry.xml new file mode 100644 index 000000000..7f8bb8087 --- /dev/null +++ b/java/com/android/dialer/calldetails/res/layout/call_details_entry.xml @@ -0,0 +1,73 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/java/com/android/dialer/calldetails/res/layout/call_details_footer.xml b/java/com/android/dialer/calldetails/res/layout/call_details_footer.xml new file mode 100644 index 000000000..885cb0989 --- /dev/null +++ b/java/com/android/dialer/calldetails/res/layout/call_details_footer.xml @@ -0,0 +1,43 @@ + + + + + + + + + + diff --git a/java/com/android/dialer/calldetails/res/layout/contact_container.xml b/java/com/android/dialer/calldetails/res/layout/contact_container.xml new file mode 100644 index 000000000..95fe189b2 --- /dev/null +++ b/java/com/android/dialer/calldetails/res/layout/contact_container.xml @@ -0,0 +1,60 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/java/com/android/dialer/calldetails/res/layout/ec_data_container.xml b/java/com/android/dialer/calldetails/res/layout/ec_data_container.xml new file mode 100644 index 000000000..5ad7912fa --- /dev/null +++ b/java/com/android/dialer/calldetails/res/layout/ec_data_container.xml @@ -0,0 +1,42 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/java/com/android/dialer/calldetails/res/menu/call_details_menu.xml b/java/com/android/dialer/calldetails/res/menu/call_details_menu.xml new file mode 100644 index 000000000..c2d1032da --- /dev/null +++ b/java/com/android/dialer/calldetails/res/menu/call_details_menu.xml @@ -0,0 +1,23 @@ + + + + + \ No newline at end of file diff --git a/java/com/android/dialer/calldetails/res/values/dimens.xml b/java/com/android/dialer/calldetails/res/values/dimens.xml new file mode 100644 index 000000000..b1a8f1c8e --- /dev/null +++ b/java/com/android/dialer/calldetails/res/values/dimens.xml @@ -0,0 +1,40 @@ + + + + 2dp + 16sp + 14sp + + + 16dp + 40dp + 16dp + 24dp + + + 24dp + 16dp + 14dp + 32dp + + + 12sp + 48dp + 72dp + 40dp + 8dp + \ No newline at end of file diff --git a/java/com/android/dialer/calldetails/res/values/strings.xml b/java/com/android/dialer/calldetails/res/values/strings.xml new file mode 100644 index 000000000..8a7cc4cfc --- /dev/null +++ b/java/com/android/dialer/calldetails/res/values/strings.xml @@ -0,0 +1,42 @@ + + + + + Call details + + + Delete + + + Copy number + + + Edit number before call + + + Call + + + Sent a photo + + + Received a photo + + + \"%1$s\" + diff --git a/java/com/android/dialer/calldetails/res/values/styles.xml b/java/com/android/dialer/calldetails/res/values/styles.xml new file mode 100644 index 000000000..4fffe1afb --- /dev/null +++ b/java/com/android/dialer/calldetails/res/values/styles.xml @@ -0,0 +1,48 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/java/com/android/dialer/callintent/nano/CallInitiationType.java b/java/com/android/dialer/callintent/nano/CallInitiationType.java index 4badd6e57..1dddb6ce8 100644 --- a/java/com/android/dialer/callintent/nano/CallInitiationType.java +++ b/java/com/android/dialer/callintent/nano/CallInitiationType.java @@ -11,17 +11,19 @@ * 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 + * limitations under the License. */ // Generated by the protocol buffer compiler. DO NOT EDIT! package com.android.dialer.callintent.nano; +/** This file is autogenerated, but javadoc required. */ @SuppressWarnings("hiding") -public final class CallInitiationType extends - com.google.protobuf.nano.ExtendableMessageNano { +public final class CallInitiationType + extends com.google.protobuf.nano.ExtendableMessageNano { + /** This file is autogenerated, but javadoc required. */ // enum Type public interface Type { public static final int UNKNOWN_INITIATION = 0; @@ -44,11 +46,11 @@ public final class CallInitiationType extends } private static volatile CallInitiationType[] _emptyArray; + public static CallInitiationType[] emptyArray() { // Lazily initializes the empty array if (_emptyArray == null) { - synchronized ( - com.google.protobuf.nano.InternalNano.LAZY_INIT_LOCK) { + synchronized (com.google.protobuf.nano.InternalNano.LAZY_INIT_LOCK) { if (_emptyArray == null) { _emptyArray = new CallInitiationType[0]; } @@ -70,20 +72,20 @@ public final class CallInitiationType extends } @Override - public CallInitiationType mergeFrom( - com.google.protobuf.nano.CodedInputByteBufferNano input) + public CallInitiationType mergeFrom(com.google.protobuf.nano.CodedInputByteBufferNano input) throws java.io.IOException { while (true) { int tag = input.readTag(); switch (tag) { case 0: return this; - default: { - if (!super.storeUnknownField(input, tag)) { - return this; + default: + { + if (!super.storeUnknownField(input, tag)) { + return this; + } + break; } - break; - } } } } @@ -94,8 +96,7 @@ public final class CallInitiationType extends } public static CallInitiationType parseFrom( - com.google.protobuf.nano.CodedInputByteBufferNano input) - throws java.io.IOException { + com.google.protobuf.nano.CodedInputByteBufferNano input) throws java.io.IOException { return new CallInitiationType().mergeFrom(input); } } diff --git a/java/com/android/dialer/calllogutils/AndroidManifest.xml b/java/com/android/dialer/calllogutils/AndroidManifest.xml new file mode 100644 index 000000000..228865a38 --- /dev/null +++ b/java/com/android/dialer/calllogutils/AndroidManifest.xml @@ -0,0 +1,16 @@ + + \ No newline at end of file diff --git a/java/com/android/dialer/calllogutils/CallEntryFormatter.java b/java/com/android/dialer/calllogutils/CallEntryFormatter.java new file mode 100644 index 000000000..bd6d53f48 --- /dev/null +++ b/java/com/android/dialer/calllogutils/CallEntryFormatter.java @@ -0,0 +1,113 @@ +/* + * 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.calllogutils; + +import android.content.Context; +import android.icu.lang.UCharacter; +import android.icu.text.BreakIterator; +import android.os.Build.VERSION; +import android.os.Build.VERSION_CODES; +import android.text.format.DateUtils; +import android.text.format.Formatter; +import com.android.dialer.util.DialerUtils; +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; + +/** Utility class for formatting data and data usage in call log entries. */ +public class CallEntryFormatter { + + /** + * Formats the provided date into a value suitable for display in the current locale. + * + *

For example, returns a string like "Wednesday, May 25, 2016, 8:02PM" or "Chorshanba, 2016 + * may 25,20:02". + * + *

For pre-N devices, the returned value may not start with a capital if the local convention + * is to not capitalize day names. On N+ devices, the returned value is always capitalized. + */ + public static CharSequence formatDate(Context context, long callDateMillis) { + CharSequence dateValue = + DateUtils.formatDateRange( + context, + callDateMillis /* startDate */, + callDateMillis /* endDate */, + DateUtils.FORMAT_SHOW_TIME + | DateUtils.FORMAT_SHOW_DATE + | DateUtils.FORMAT_SHOW_WEEKDAY + | DateUtils.FORMAT_SHOW_YEAR); + + // We want the beginning of the date string to be capitalized, even if the word at the beginning + // of the string is not usually capitalized. For example, "Wednesdsay" in Uzbek is "chorshanba” + // (not capitalized). To handle this issue we apply title casing to the start of the sentence so + // that "chorshanba, 2016 may 25,20:02" becomes "Chorshanba, 2016 may 25,20:02". + // + // The ICU library was not available in Android until N, so we can only do this in N+ devices. + // Pre-N devices will still see incorrect capitalization in some languages. + if (VERSION.SDK_INT < VERSION_CODES.N) { + return dateValue; + } + + // Using the ICU library is safer than just applying toUpperCase() on the first letter of the + // word because in some languages, there can be multiple starting characters which should be + // upper-cased together. For example in Dutch "ij" is a digraph in which both letters should be + // capitalized together. + + // TITLECASE_NO_LOWERCASE is necessary so that things that are already capitalized like the + // month ("May") are not lower-cased as part of the conversion. + return UCharacter.toTitleCase( + Locale.getDefault(), + dateValue.toString(), + BreakIterator.getSentenceInstance(), + UCharacter.TITLECASE_NO_LOWERCASE); + } + + private static CharSequence formatDuration(Context context, long elapsedSeconds) { + long minutes = 0; + long seconds = 0; + + if (elapsedSeconds >= 60) { + minutes = elapsedSeconds / 60; + elapsedSeconds -= minutes * 60; + seconds = elapsedSeconds; + return context.getString(R.string.call_details_duration_format, minutes, seconds); + } else { + seconds = elapsedSeconds; + return context.getString(R.string.call_details_short_duration_format, seconds); + } + } + + /** + * Formats a string containing the call duration and the data usage (if specified). + * + * @param elapsedSeconds Total elapsed seconds. + * @param dataUsage Data usage in bytes, or null if not specified. + * @return String containing call duration and data usage. + */ + public static CharSequence formatDurationAndDataUsage( + Context context, long elapsedSeconds, Long dataUsage) { + CharSequence duration = formatDuration(context, elapsedSeconds); + List durationItems = new ArrayList<>(); + if (dataUsage != null) { + durationItems.add(duration); + durationItems.add(Formatter.formatShortFileSize(context, dataUsage)); + return DialerUtils.join(durationItems); + } else { + return duration; + } + } +} diff --git a/java/com/android/dialer/calllogutils/CallTypeHelper.java b/java/com/android/dialer/calllogutils/CallTypeHelper.java new file mode 100644 index 000000000..d3b5b67d7 --- /dev/null +++ b/java/com/android/dialer/calllogutils/CallTypeHelper.java @@ -0,0 +1,135 @@ +/* + * Copyright (C) 2011 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.calllogutils; + +import android.content.res.Resources; +import com.android.dialer.compat.AppCompatConstants; + +/** Helper class to perform operations related to call types. */ +public class CallTypeHelper { + + /** Name used to identify incoming calls. */ + private final CharSequence mIncomingName; + /** Name used to identify incoming calls which were transferred to another device. */ + private final CharSequence mIncomingPulledName; + /** Name used to identify outgoing calls. */ + private final CharSequence mOutgoingName; + /** Name used to identify outgoing calls which were transferred to another device. */ + private final CharSequence mOutgoingPulledName; + /** Name used to identify missed calls. */ + private final CharSequence mMissedName; + /** Name used to identify incoming video calls. */ + private final CharSequence mIncomingVideoName; + /** Name used to identify incoming video calls which were transferred to another device. */ + private final CharSequence mIncomingVideoPulledName; + /** Name used to identify outgoing video calls. */ + private final CharSequence mOutgoingVideoName; + /** Name used to identify outgoing video calls which were transferred to another device. */ + private final CharSequence mOutgoingVideoPulledName; + /** Name used to identify missed video calls. */ + private final CharSequence mMissedVideoName; + /** Name used to identify voicemail calls. */ + private final CharSequence mVoicemailName; + /** Name used to identify rejected calls. */ + private final CharSequence mRejectedName; + /** Name used to identify blocked calls. */ + private final CharSequence mBlockedName; + /** Name used to identify calls which were answered on another device. */ + private final CharSequence mAnsweredElsewhereName; + + public CallTypeHelper(Resources resources) { + // Cache these values so that we do not need to look them up each time. + mIncomingName = resources.getString(R.string.type_incoming); + mIncomingPulledName = resources.getString(R.string.type_incoming_pulled); + mOutgoingName = resources.getString(R.string.type_outgoing); + mOutgoingPulledName = resources.getString(R.string.type_outgoing_pulled); + mMissedName = resources.getString(R.string.type_missed); + mIncomingVideoName = resources.getString(R.string.type_incoming_video); + mIncomingVideoPulledName = resources.getString(R.string.type_incoming_video_pulled); + mOutgoingVideoName = resources.getString(R.string.type_outgoing_video); + mOutgoingVideoPulledName = resources.getString(R.string.type_outgoing_video_pulled); + mMissedVideoName = resources.getString(R.string.type_missed_video); + mVoicemailName = resources.getString(R.string.type_voicemail); + mRejectedName = resources.getString(R.string.type_rejected); + mBlockedName = resources.getString(R.string.type_blocked); + mAnsweredElsewhereName = resources.getString(R.string.type_answered_elsewhere); + } + + public static boolean isMissedCallType(int callType) { + return (callType != AppCompatConstants.CALLS_INCOMING_TYPE + && callType != AppCompatConstants.CALLS_OUTGOING_TYPE + && callType != AppCompatConstants.CALLS_VOICEMAIL_TYPE + && callType != AppCompatConstants.CALLS_ANSWERED_EXTERNALLY_TYPE); + } + + /** Returns the text used to represent the given call type. */ + public CharSequence getCallTypeText(int callType, boolean isVideoCall, boolean isPulledCall) { + switch (callType) { + case AppCompatConstants.CALLS_INCOMING_TYPE: + if (isVideoCall) { + if (isPulledCall) { + return mIncomingVideoPulledName; + } else { + return mIncomingVideoName; + } + } else { + if (isPulledCall) { + return mIncomingPulledName; + } else { + return mIncomingName; + } + } + + case AppCompatConstants.CALLS_OUTGOING_TYPE: + if (isVideoCall) { + if (isPulledCall) { + return mOutgoingVideoPulledName; + } else { + return mOutgoingVideoName; + } + } else { + if (isPulledCall) { + return mOutgoingPulledName; + } else { + return mOutgoingName; + } + } + + case AppCompatConstants.CALLS_MISSED_TYPE: + if (isVideoCall) { + return mMissedVideoName; + } else { + return mMissedName; + } + + case AppCompatConstants.CALLS_VOICEMAIL_TYPE: + return mVoicemailName; + + case AppCompatConstants.CALLS_REJECTED_TYPE: + return mRejectedName; + + case AppCompatConstants.CALLS_BLOCKED_TYPE: + return mBlockedName; + + case AppCompatConstants.CALLS_ANSWERED_EXTERNALLY_TYPE: + return mAnsweredElsewhereName; + + default: + return mMissedName; + } + } +} diff --git a/java/com/android/dialer/calllogutils/CallTypeIconsView.java b/java/com/android/dialer/calllogutils/CallTypeIconsView.java new file mode 100644 index 000000000..61208bc9a --- /dev/null +++ b/java/com/android/dialer/calllogutils/CallTypeIconsView.java @@ -0,0 +1,244 @@ +/* + * Copyright (C) 2011 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.calllogutils; + +import android.content.Context; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.graphics.Canvas; +import android.graphics.PorterDuff; +import android.graphics.drawable.BitmapDrawable; +import android.graphics.drawable.Drawable; +import android.util.AttributeSet; +import android.view.View; +import com.android.contacts.common.util.BitmapUtil; +import com.android.dialer.compat.AppCompatConstants; +import java.util.ArrayList; +import java.util.List; + +/** + * View that draws one or more symbols for different types of calls (missed calls, outgoing etc). + * The symbols are set up horizontally. As this view doesn't create subviews, it is better suited + * for ListView-recycling that a regular LinearLayout using ImageViews. + */ +public class CallTypeIconsView extends View { + + private static Resources sResources; + private List mCallTypes = new ArrayList<>(3); + private boolean mShowVideo = false; + private boolean mShowHd = false; + private int mWidth; + private int mHeight; + + public CallTypeIconsView(Context context) { + this(context, null); + } + + public CallTypeIconsView(Context context, AttributeSet attrs) { + super(context, attrs); + if (sResources == null) { + sResources = new Resources(context); + } + } + + public void clear() { + mCallTypes.clear(); + mWidth = 0; + mHeight = 0; + invalidate(); + } + + public void add(int callType) { + mCallTypes.add(callType); + + final Drawable drawable = getCallTypeDrawable(callType); + mWidth += drawable.getIntrinsicWidth() + sResources.iconMargin; + mHeight = Math.max(mHeight, drawable.getIntrinsicHeight()); + invalidate(); + } + + /** + * Determines whether the video call icon will be shown. + * + * @param showVideo True where the video icon should be shown. + */ + public void setShowVideo(boolean showVideo) { + mShowVideo = showVideo; + if (showVideo) { + mWidth += sResources.videoCall.getIntrinsicWidth(); + mHeight = Math.max(mHeight, sResources.videoCall.getIntrinsicHeight()); + invalidate(); + } + } + + /** + * Determines if the video icon should be shown. + * + * @return True if the video icon should be shown. + */ + public boolean isVideoShown() { + return mShowVideo; + } + + public void setShowHd(boolean showHd) { + mShowHd = showHd; + if (showHd) { + mWidth += sResources.hdCall.getIntrinsicWidth(); + mHeight = Math.max(mHeight, sResources.hdCall.getIntrinsicHeight()); + invalidate(); + } + } + + public int getCount() { + return mCallTypes.size(); + } + + public int getCallType(int index) { + return mCallTypes.get(index); + } + + private Drawable getCallTypeDrawable(int callType) { + switch (callType) { + case AppCompatConstants.CALLS_INCOMING_TYPE: + case AppCompatConstants.CALLS_ANSWERED_EXTERNALLY_TYPE: + return sResources.incoming; + case AppCompatConstants.CALLS_OUTGOING_TYPE: + return sResources.outgoing; + case AppCompatConstants.CALLS_MISSED_TYPE: + return sResources.missed; + case AppCompatConstants.CALLS_VOICEMAIL_TYPE: + return sResources.voicemail; + case AppCompatConstants.CALLS_BLOCKED_TYPE: + return sResources.blocked; + default: + // It is possible for users to end up with calls with unknown call types in their + // call history, possibly due to 3rd party call log implementations (e.g. to + // distinguish between rejected and missed calls). Instead of crashing, just + // assume that all unknown call types are missed calls. + return sResources.missed; + } + } + + @Override + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + setMeasuredDimension(mWidth, mHeight); + } + + @Override + protected void onDraw(Canvas canvas) { + int left = 0; + for (Integer callType : mCallTypes) { + final Drawable drawable = getCallTypeDrawable(callType); + final int right = left + drawable.getIntrinsicWidth(); + drawable.setBounds(left, 0, right, drawable.getIntrinsicHeight()); + drawable.draw(canvas); + left = right + sResources.iconMargin; + } + + // If showing the video call icon, draw it scaled appropriately. + if (mShowVideo) { + final Drawable drawable = sResources.videoCall; + final int right = left + sResources.videoCall.getIntrinsicWidth(); + drawable.setBounds(left, 0, right, sResources.videoCall.getIntrinsicHeight()); + drawable.draw(canvas); + } + // If showing HD call icon, draw it scaled appropriately. + if (mShowHd) { + final Drawable drawable = sResources.hdCall; + final int right = left + sResources.hdCall.getIntrinsicWidth(); + drawable.setBounds(left, 0, right, sResources.hdCall.getIntrinsicHeight()); + drawable.draw(canvas); + } + } + + private static class Resources { + + // Drawable representing an incoming answered call. + public final Drawable incoming; + + // Drawable respresenting an outgoing call. + public final Drawable outgoing; + + // Drawable representing an incoming missed call. + public final Drawable missed; + + // Drawable representing a voicemail. + public final Drawable voicemail; + + // Drawable representing a blocked call. + public final Drawable blocked; + + // Drawable repesenting a video call. + public final Drawable videoCall; + + // Drawable represeting a hd call. + public final Drawable hdCall; + + /** The margin to use for icons. */ + public final int iconMargin; + + /** + * Configures the call icon drawables. A single white call arrow which points down and left is + * used as a basis for all of the call arrow icons, applying rotation and colors as needed. + * + * @param context The current context. + */ + public Resources(Context context) { + final android.content.res.Resources r = context.getResources(); + + incoming = r.getDrawable(R.drawable.ic_call_arrow); + incoming.setColorFilter(r.getColor(R.color.answered_call), PorterDuff.Mode.MULTIPLY); + + // Create a rotated instance of the call arrow for outgoing calls. + outgoing = BitmapUtil.getRotatedDrawable(r, R.drawable.ic_call_arrow, 180f); + outgoing.setColorFilter(r.getColor(R.color.answered_call), PorterDuff.Mode.MULTIPLY); + + // Need to make a copy of the arrow drawable, otherwise the same instance colored + // above will be recolored here. + missed = r.getDrawable(R.drawable.ic_call_arrow).mutate(); + missed.setColorFilter(r.getColor(R.color.missed_call), PorterDuff.Mode.MULTIPLY); + + voicemail = r.getDrawable(R.drawable.quantum_ic_voicemail_white_18); + voicemail.setColorFilter( + r.getColor(R.color.dialer_secondary_text_color), PorterDuff.Mode.MULTIPLY); + + blocked = getScaledBitmap(context, R.drawable.ic_block_24dp); + blocked.setColorFilter(r.getColor(R.color.blocked_call), PorterDuff.Mode.MULTIPLY); + + videoCall = getScaledBitmap(context, R.drawable.quantum_ic_videocam_white_24); + videoCall.setColorFilter( + r.getColor(R.color.dialer_secondary_text_color), PorterDuff.Mode.MULTIPLY); + + hdCall = getScaledBitmap(context, R.drawable.quantum_ic_hd_white_24); + hdCall.setColorFilter( + r.getColor(R.color.dialer_secondary_text_color), PorterDuff.Mode.MULTIPLY); + + iconMargin = r.getDimensionPixelSize(R.dimen.call_log_icon_margin); + } + + // Gets the icon, scaled to the height of the call type icons. This helps display all the + // icons to be the same height, while preserving their width aspect ratio. + private Drawable getScaledBitmap(Context context, int resourceId) { + Bitmap icon = BitmapFactory.decodeResource(context.getResources(), resourceId); + int scaledHeight = context.getResources().getDimensionPixelSize(R.dimen.call_type_icon_size); + int scaledWidth = + (int) ((float) icon.getWidth() * ((float) scaledHeight / (float) icon.getHeight())); + Bitmap scaledIcon = Bitmap.createScaledBitmap(icon, scaledWidth, scaledHeight, false); + return new BitmapDrawable(context.getResources(), scaledIcon); + } + } +} diff --git a/java/com/android/dialer/calllogutils/PhoneAccountUtils.java b/java/com/android/dialer/calllogutils/PhoneAccountUtils.java new file mode 100644 index 000000000..c639893ef --- /dev/null +++ b/java/com/android/dialer/calllogutils/PhoneAccountUtils.java @@ -0,0 +1,104 @@ +/* + * Copyright (C) 2013 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.calllogutils; + +import android.content.ComponentName; +import android.content.Context; +import android.support.annotation.Nullable; +import android.telecom.PhoneAccount; +import android.telecom.PhoneAccountHandle; +import android.text.TextUtils; +import com.android.dialer.telecom.TelecomUtil; +import java.util.ArrayList; +import java.util.List; + +/** Methods to help extract {@code PhoneAccount} information from database and Telecomm sources. */ +public class PhoneAccountUtils { + + /** Return a list of phone accounts that are subscription/SIM accounts. */ + public static List getSubscriptionPhoneAccounts(Context context) { + List subscriptionAccountHandles = new ArrayList(); + final List accountHandles = + TelecomUtil.getCallCapablePhoneAccounts(context); + for (PhoneAccountHandle accountHandle : accountHandles) { + PhoneAccount account = TelecomUtil.getPhoneAccount(context, accountHandle); + if (account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) { + subscriptionAccountHandles.add(accountHandle); + } + } + return subscriptionAccountHandles; + } + + /** Compose PhoneAccount object from component name and account id. */ + @Nullable + public static PhoneAccountHandle getAccount( + @Nullable String componentString, @Nullable String accountId) { + if (TextUtils.isEmpty(componentString) || TextUtils.isEmpty(accountId)) { + return null; + } + final ComponentName componentName = ComponentName.unflattenFromString(componentString); + if (componentName == null) { + return null; + } + return new PhoneAccountHandle(componentName, accountId); + } + + /** Extract account label from PhoneAccount object. */ + @Nullable + public static String getAccountLabel( + Context context, @Nullable PhoneAccountHandle accountHandle) { + PhoneAccount account = getAccountOrNull(context, accountHandle); + if (account != null && account.getLabel() != null) { + return account.getLabel().toString(); + } + return null; + } + + /** Extract account color from PhoneAccount object. */ + public static int getAccountColor(Context context, @Nullable PhoneAccountHandle accountHandle) { + final PhoneAccount account = TelecomUtil.getPhoneAccount(context, accountHandle); + + // For single-sim devices the PhoneAccount will be NO_HIGHLIGHT_COLOR by default, so it is + // safe to always use the account highlight color. + return account == null ? PhoneAccount.NO_HIGHLIGHT_COLOR : account.getHighlightColor(); + } + + /** + * Determine whether a phone account supports call subjects. + * + * @return {@code true} if call subjects are supported, {@code false} otherwise. + */ + public static boolean getAccountSupportsCallSubject( + Context context, @Nullable PhoneAccountHandle accountHandle) { + final PhoneAccount account = TelecomUtil.getPhoneAccount(context, accountHandle); + + return account != null && account.hasCapabilities(PhoneAccount.CAPABILITY_CALL_SUBJECT); + } + + /** + * Retrieve the account metadata, but if the account does not exist or the device has only a + * single registered and enabled account, return null. + */ + @Nullable + private static PhoneAccount getAccountOrNull( + Context context, @Nullable PhoneAccountHandle accountHandle) { + if (TelecomUtil.getCallCapablePhoneAccounts(context).size() <= 1) { + return null; + } + return TelecomUtil.getPhoneAccount(context, accountHandle); + } +} diff --git a/java/com/android/dialer/calllogutils/PhoneCallDetails.java b/java/com/android/dialer/calllogutils/PhoneCallDetails.java new file mode 100644 index 000000000..ba05a87e2 --- /dev/null +++ b/java/com/android/dialer/calllogutils/PhoneCallDetails.java @@ -0,0 +1,206 @@ +/* + * Copyright (C) 2011 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.calllogutils; + +import android.content.Context; +import android.content.res.Resources; +import android.net.Uri; +import android.provider.CallLog; +import android.provider.CallLog.Calls; +import android.support.annotation.Nullable; +import android.telecom.PhoneAccountHandle; +import android.text.TextUtils; +import com.android.contacts.common.ContactsUtils.UserType; +import com.android.contacts.common.preference.ContactsPreferences; +import com.android.contacts.common.util.ContactDisplayUtils; +import com.android.dialer.phonenumbercache.ContactInfo; + +/** The details of a phone call to be shown in the UI. */ +public class PhoneCallDetails { + + // The number of the other party involved in the call. + public CharSequence number; + // Post-dial digits associated with the outgoing call. + public String postDialDigits; + // The secondary line number the call was received via. + public String viaNumber; + // The number presenting rules set by the network, e.g., {@link Calls#PRESENTATION_ALLOWED} + public int numberPresentation; + // The country corresponding with the phone number. + public String countryIso; + // The geocoded location for the phone number. + public String geocode; + + /** + * The type of calls, as defined in the call log table, e.g., {@link Calls#INCOMING_TYPE}. + * + *

There might be multiple types if this represents a set of entries grouped together. + */ + public int[] callTypes; + + // The date of the call, in milliseconds since the epoch. + public long date; + // The duration of the call in milliseconds, or 0 for missed calls. + public long duration; + // The name of the contact, or the empty string. + public CharSequence namePrimary; + // The alternative name of the contact, e.g. last name first, or the empty string + public CharSequence nameAlternative; + /** + * The user's preference on name display order, last name first or first time first. {@see + * ContactsPreferences} + */ + public int nameDisplayOrder; + // The type of phone, e.g., {@link Phone#TYPE_HOME}, 0 if not available. + public int numberType; + // The custom label associated with the phone number in the contact, or the empty string. + public CharSequence numberLabel; + // The URI of the contact associated with this phone call. + public Uri contactUri; + + /** + * The photo URI of the picture of the contact that is associated with this phone call or null if + * there is none. + * + *

This is meant to store the high-res photo only. + */ + public Uri photoUri; + + // The source type of the contact associated with this call. + public int sourceType; + + // The object id type of the contact associated with this call. + public String objectId; + + // The unique identifier for the account associated with the call. + public PhoneAccountHandle accountHandle; + + // Features applicable to this call. + public int features; + + // Total data usage for this call. + public Long dataUsage; + + // Voicemail transcription + public String transcription; + + // The display string for the number. + public String displayNumber; + + // Whether the contact number is a voicemail number. + public boolean isVoicemail; + + /** The {@link UserType} of the contact */ + public @UserType long contactUserType; + + /** + * If this is a voicemail, whether the message is read. For other types of calls, this defaults to + * {@code true}. + */ + public boolean isRead = true; + + // If this call is a spam number. + public boolean isSpam = false; + + // If this call is a blocked number. + public boolean isBlocked = false; + + // Call location and date text. + public CharSequence callLocationAndDate; + + // Call description. + public CharSequence callDescription; + public String accountComponentName; + public String accountId; + public ContactInfo cachedContactInfo; + public int voicemailId; + public int previousGroup; + + /** + * Constructor with required fields for the details of a call with a number associated with a + * contact. + */ + public PhoneCallDetails( + CharSequence number, int numberPresentation, CharSequence postDialDigits) { + this.number = number; + this.numberPresentation = numberPresentation; + this.postDialDigits = postDialDigits.toString(); + } + /** + * Construct the "on {accountLabel} via {viaNumber}" accessibility description for the account + * list item, depending on the existence of the accountLabel and viaNumber. + * + * @param viaNumber The number that this call is being placed via. + * @param accountLabel The {@link PhoneAccount} label that this call is being placed with. + * @return The description of the account that this call has been placed on. + */ + public static CharSequence createAccountLabelDescription( + Resources resources, @Nullable String viaNumber, @Nullable CharSequence accountLabel) { + + if ((!TextUtils.isEmpty(viaNumber)) && !TextUtils.isEmpty(accountLabel)) { + String msg = + resources.getString( + R.string.description_via_number_phone_account, accountLabel, viaNumber); + CharSequence accountNumberLabel = + ContactDisplayUtils.getTelephoneTtsSpannable(msg, viaNumber); + return (accountNumberLabel == null) ? msg : accountNumberLabel; + } else if (!TextUtils.isEmpty(viaNumber)) { + CharSequence viaNumberLabel = + ContactDisplayUtils.getTtsSpannedPhoneNumber( + resources, R.string.description_via_number, viaNumber); + return (viaNumberLabel == null) ? viaNumber : viaNumberLabel; + } else if (!TextUtils.isEmpty(accountLabel)) { + return TextUtils.expandTemplate( + resources.getString(R.string.description_phone_account), accountLabel); + } + return ""; + } + + /** + * Returns the preferred name for the call details as specified by the {@link #nameDisplayOrder} + * + * @return the preferred name + */ + public CharSequence getPreferredName() { + if (nameDisplayOrder == ContactsPreferences.DISPLAY_ORDER_PRIMARY + || TextUtils.isEmpty(nameAlternative)) { + return namePrimary; + } + return nameAlternative; + } + + public void updateDisplayNumber( + Context context, CharSequence formattedNumber, boolean isVoicemail) { + displayNumber = + PhoneNumberDisplayUtil.getDisplayNumber( + context, number, numberPresentation, formattedNumber, postDialDigits, isVoicemail) + .toString(); + } + + public boolean hasIncomingCalls() { + for (int i = 0; i < callTypes.length; i++) { + if (callTypes[i] == CallLog.Calls.INCOMING_TYPE + || callTypes[i] == CallLog.Calls.MISSED_TYPE + || callTypes[i] == CallLog.Calls.VOICEMAIL_TYPE + || callTypes[i] == CallLog.Calls.REJECTED_TYPE + || callTypes[i] == CallLog.Calls.BLOCKED_TYPE) { + return true; + } + } + return false; + } +} diff --git a/java/com/android/dialer/calllogutils/PhoneNumberDisplayUtil.java b/java/com/android/dialer/calllogutils/PhoneNumberDisplayUtil.java new file mode 100644 index 000000000..9bebfacac --- /dev/null +++ b/java/com/android/dialer/calllogutils/PhoneNumberDisplayUtil.java @@ -0,0 +1,83 @@ +/* + * Copyright (C) 2011 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.calllogutils; + +import android.content.Context; +import android.provider.CallLog.Calls; +import android.text.BidiFormatter; +import android.text.TextDirectionHeuristics; +import android.text.TextUtils; +import com.android.contacts.common.compat.PhoneNumberUtilsCompat; +import com.android.dialer.phonenumberutil.PhoneNumberHelper; + +/** Helper for formatting and managing the display of phone numbers. */ +public class PhoneNumberDisplayUtil { + + /** Returns the string to display for the given phone number if there is no matching contact. */ + public static CharSequence getDisplayName( + Context context, CharSequence number, int presentation, boolean isVoicemail) { + if (presentation == Calls.PRESENTATION_UNKNOWN) { + return context.getResources().getString(R.string.unknown); + } + if (presentation == Calls.PRESENTATION_RESTRICTED) { + return PhoneNumberHelper.getDisplayNameForRestrictedNumber(context); + } + if (presentation == Calls.PRESENTATION_PAYPHONE) { + return context.getResources().getString(R.string.payphone); + } + if (isVoicemail) { + return context.getResources().getString(R.string.voicemail_string); + } + if (PhoneNumberHelper.isLegacyUnknownNumbers(number)) { + return context.getResources().getString(R.string.unknown); + } + return ""; + } + + /** + * Returns the string to display for the given phone number. + * + * @param number the number to display + * @param formattedNumber the formatted number if available, may be null + */ + public static CharSequence getDisplayNumber( + Context context, + CharSequence number, + int presentation, + CharSequence formattedNumber, + CharSequence postDialDigits, + boolean isVoicemail) { + final CharSequence displayName = getDisplayName(context, number, presentation, isVoicemail); + if (!TextUtils.isEmpty(displayName)) { + return getTtsSpannableLtrNumber(displayName); + } + + if (!TextUtils.isEmpty(formattedNumber)) { + return getTtsSpannableLtrNumber(formattedNumber); + } else if (!TextUtils.isEmpty(number)) { + return getTtsSpannableLtrNumber(number.toString() + postDialDigits); + } else { + return context.getResources().getString(R.string.unknown); + } + } + + /** Returns number annotated as phone number in LTR direction. */ + public static CharSequence getTtsSpannableLtrNumber(CharSequence number) { + return PhoneNumberUtilsCompat.createTtsSpannable( + BidiFormatter.getInstance().unicodeWrap(number.toString(), TextDirectionHeuristics.LTR)); + } +} diff --git a/java/com/android/dialer/calllogutils/res/values/colors.xml b/java/com/android/dialer/calllogutils/res/values/colors.xml new file mode 100644 index 000000000..dc4ec2493 --- /dev/null +++ b/java/com/android/dialer/calllogutils/res/values/colors.xml @@ -0,0 +1,24 @@ + + + + + #ff2e58 + + #00c853 + + @color/dialer_secondary_text_color + \ No newline at end of file diff --git a/java/com/android/dialer/calllogutils/res/values/dimens.xml b/java/com/android/dialer/calllogutils/res/values/dimens.xml new file mode 100644 index 000000000..0935ac188 --- /dev/null +++ b/java/com/android/dialer/calllogutils/res/values/dimens.xml @@ -0,0 +1,19 @@ + + + + 12dp + \ No newline at end of file diff --git a/java/com/android/dialer/calllogutils/res/values/strings.xml b/java/com/android/dialer/calllogutils/res/values/strings.xml new file mode 100644 index 000000000..6a6f10113 --- /dev/null +++ b/java/com/android/dialer/calllogutils/res/values/strings.xml @@ -0,0 +1,90 @@ + + + + + Incoming call + + + Incoming call transferred to another device + + + Outgoing call + + + Outgoing call transferred to another device + + + Missed call + + + Incoming video call + + + Incoming video call transferred to another device + + + Outgoing video call + + + Outgoing video call transferred to another device + + + Missed video call + + + Voicemail + + + Declined call + + + Blocked call + + + Call answered on another device + + + on ^1 + + + via %1$s + + + on %1$s, via %2$s + + + Voicemail + + + %s min %s sec + + + %s sec + \ No newline at end of file diff --git a/java/com/android/dialer/common/Assert.java b/java/com/android/dialer/common/Assert.java index 00b4f2595..943e1ddcf 100644 --- a/java/com/android/dialer/common/Assert.java +++ b/java/com/android/dialer/common/Assert.java @@ -19,6 +19,7 @@ package com.android.dialer.common; import android.os.Looper; import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import javax.annotation.CheckReturnValue; /** Assertions which will result in program termination unless disabled by flags. */ public class Assert { @@ -33,7 +34,9 @@ public class Assert { * Called when a truly exceptional case occurs. * * @throws AssertionError + * @deprecated Use throw Assert.create*FailException() instead. */ + @Deprecated public static void fail() { throw new AssertionError("Fail"); } @@ -43,11 +46,38 @@ public class Assert { * * @param reason the optional reason to supply as the exception message * @throws AssertionError + * @deprecated Use throw Assert.create*FailException() instead. */ + @Deprecated public static void fail(String reason) { throw new AssertionError(reason); } + @CheckReturnValue + public static AssertionError createAssertionFailException(String msg) { + return new AssertionError(msg); + } + + @CheckReturnValue + public static UnsupportedOperationException createUnsupportedOperationFailException() { + return new UnsupportedOperationException(); + } + + @CheckReturnValue + public static UnsupportedOperationException createUnsupportedOperationFailException(String msg) { + return new UnsupportedOperationException(msg); + } + + @CheckReturnValue + public static IllegalStateException createIllegalStateFailException() { + return new IllegalStateException(); + } + + @CheckReturnValue + public static IllegalStateException createIllegalStateFailException(String msg) { + return new IllegalStateException(msg); + } + /** * Ensures the truth of an expression involving one or more parameters to the calling method. * diff --git a/java/com/android/dialer/common/AutoValue_FallibleAsyncTask_FallibleTaskResult.java b/java/com/android/dialer/common/AutoValue_FallibleAsyncTask_FallibleTaskResult.java deleted file mode 100644 index f9d7cea90..000000000 --- a/java/com/android/dialer/common/AutoValue_FallibleAsyncTask_FallibleTaskResult.java +++ /dev/null @@ -1,79 +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.dialer.common; - -import android.support.annotation.Nullable; -import javax.annotation.Generated; - - - final class AutoValue_FallibleAsyncTask_FallibleTaskResult extends FallibleAsyncTask.FallibleTaskResult { - - private final Throwable throwable; - private final ResultT result; - - AutoValue_FallibleAsyncTask_FallibleTaskResult( - @Nullable Throwable throwable, - @Nullable ResultT result) { - this.throwable = throwable; - this.result = result; - } - - @Nullable - @Override - public Throwable getThrowable() { - return throwable; - } - - @Nullable - @Override - public ResultT getResult() { - return result; - } - - @Override - public String toString() { - return "FallibleTaskResult{" - + "throwable=" + throwable + ", " - + "result=" + result - + "}"; - } - - @Override - public boolean equals(Object o) { - if (o == this) { - return true; - } - if (o instanceof FallibleAsyncTask.FallibleTaskResult) { - FallibleAsyncTask.FallibleTaskResult that = (FallibleAsyncTask.FallibleTaskResult) o; - return ((this.throwable == null) ? (that.getThrowable() == null) : this.throwable.equals(that.getThrowable())) - && ((this.result == null) ? (that.getResult() == null) : this.result.equals(that.getResult())); - } - return false; - } - - @Override - public int hashCode() { - int h = 1; - h *= 1000003; - h ^= (throwable == null) ? 0 : this.throwable.hashCode(); - h *= 1000003; - h ^= (result == null) ? 0 : this.result.hashCode(); - return h; - } - -} - diff --git a/java/com/android/dialer/common/FallibleAsyncTask.java b/java/com/android/dialer/common/FallibleAsyncTask.java index fbdbda75f..f3abace1a 100644 --- a/java/com/android/dialer/common/FallibleAsyncTask.java +++ b/java/com/android/dialer/common/FallibleAsyncTask.java @@ -20,7 +20,7 @@ import android.os.AsyncTask; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import com.android.dialer.common.FallibleAsyncTask.FallibleTaskResult; - +import com.google.auto.value.AutoValue; /** * A task that runs work in the background, passing Throwables from {@link @@ -52,8 +52,8 @@ public abstract class FallibleAsyncTask * * @param the type of the result of the background computation */ - - protected abstract static class FallibleTaskResult { + @AutoValue + public abstract static class FallibleTaskResult { /** Creates an instance of FallibleTaskResult for the given throwable. */ private static FallibleTaskResult createFailureResult(@NonNull Throwable t) { diff --git a/java/com/android/dialer/common/PerAccountSharedPreferences.java b/java/com/android/dialer/common/PerAccountSharedPreferences.java new file mode 100644 index 000000000..0ed1b03a5 --- /dev/null +++ b/java/com/android/dialer/common/PerAccountSharedPreferences.java @@ -0,0 +1,146 @@ +/* + * 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.common; + +import android.content.Context; +import android.content.SharedPreferences; +import android.support.annotation.Nullable; +import android.telecom.PhoneAccountHandle; +import java.util.Set; + +/** + * Class that helps us store dialer preferences that are phone account dependent. This is necessary + * for cases such as settings that are phone account dependent e.g endless vm. The logic is to + * essentially store the shared preference by appending the phone account id to the key. + */ +public class PerAccountSharedPreferences { + private final String sharedPrefsKeyPrefix; + private final SharedPreferences preferences; + private final PhoneAccountHandle phoneAccountHandle; + + public PerAccountSharedPreferences( + Context context, PhoneAccountHandle handle, SharedPreferences prefs) { + preferences = prefs; + phoneAccountHandle = handle; + sharedPrefsKeyPrefix = "phone_account_dependent_"; + } + + /** + * Not to be used, currently only used by {@VisualVoicemailPreferences} for legacy reasons. + */ + protected PerAccountSharedPreferences( + Context context, PhoneAccountHandle handle, SharedPreferences prefs, String prefix) { + Assert.checkArgument(prefix.equals("visual_voicemail_")); + preferences = prefs; + phoneAccountHandle = handle; + sharedPrefsKeyPrefix = prefix; + } + + public class Editor { + + private final SharedPreferences.Editor editor; + + private Editor() { + editor = preferences.edit(); + } + + public void apply() { + editor.apply(); + } + + public Editor putBoolean(String key, boolean value) { + editor.putBoolean(getKey(key), value); + return this; + } + + public Editor putFloat(String key, float value) { + editor.putFloat(getKey(key), value); + return this; + } + + public Editor putInt(String key, int value) { + editor.putInt(getKey(key), value); + return this; + } + + public Editor putLong(String key, long value) { + editor.putLong(getKey(key), value); + return this; + } + + public Editor putString(String key, String value) { + editor.putString(getKey(key), value); + return this; + } + + public Editor putStringSet(String key, Set value) { + editor.putStringSet(getKey(key), value); + return this; + } + } + + public Editor edit() { + return new Editor(); + } + + public boolean getBoolean(String key, boolean defValue) { + return getValue(key, defValue); + } + + public float getFloat(String key, float defValue) { + return getValue(key, defValue); + } + + public int getInt(String key, int defValue) { + return getValue(key, defValue); + } + + public long getLong(String key, long defValue) { + return getValue(key, defValue); + } + + public String getString(String key, String defValue) { + return getValue(key, defValue); + } + + @Nullable + public String getString(String key) { + return getValue(key, null); + } + + public Set getStringSet(String key, Set defValue) { + return getValue(key, defValue); + } + + public boolean contains(String key) { + return preferences.contains(getKey(key)); + } + + private T getValue(String key, T defValue) { + if (!contains(key)) { + return defValue; + } + Object object = preferences.getAll().get(getKey(key)); + if (object == null) { + return defValue; + } + return (T) object; + } + + private String getKey(String key) { + return sharedPrefsKeyPrefix + key + "_" + phoneAccountHandle.getId(); + } +} diff --git a/java/com/android/dialer/common/proguard.flags b/java/com/android/dialer/common/proguard.flags new file mode 100644 index 000000000..4b6b84671 --- /dev/null +++ b/java/com/android/dialer/common/proguard.flags @@ -0,0 +1,4 @@ +-assumenosideeffects class com.android.dialer.common.LogUtil { + public static void v(...); + public static void d(...); +} diff --git a/java/com/android/dialer/common/res/values/config.xml b/java/com/android/dialer/common/res/values/config.xml new file mode 100644 index 000000000..c4df279ba --- /dev/null +++ b/java/com/android/dialer/common/res/values/config.xml @@ -0,0 +1,4 @@ + + + false + \ No newline at end of file diff --git a/java/com/android/dialer/constants/Constants.java b/java/com/android/dialer/constants/Constants.java index 77773018a..d92c0bcfc 100644 --- a/java/com/android/dialer/constants/Constants.java +++ b/java/com/android/dialer/constants/Constants.java @@ -19,7 +19,6 @@ package com.android.dialer.constants; import android.support.annotation.NonNull; import com.android.dialer.common.Assert; import com.android.dialer.proguard.UsedByReflection; -import com.android.dialer.constants.ConstantsImpl; /** * Utility to access constants that are different across build variants (Google Dialer, AOSP, @@ -29,11 +28,22 @@ import com.android.dialer.constants.ConstantsImpl; */ @UsedByReflection(value = "Constants.java") public abstract class Constants { - private static Constants instance = new ConstantsImpl(); + private static Constants instance; private static boolean didInitializeInstance; @NonNull public static synchronized Constants get() { + if (!didInitializeInstance) { + didInitializeInstance = true; + try { + Class clazz = Class.forName(Constants.class.getName() + "Impl"); + instance = (Constants) clazz.getConstructor().newInstance(); + } catch (ReflectiveOperationException e) { + Assert.fail( + "Unable to create an instance of ConstantsImpl. To fix this error include one of the " + + "constants modules (googledialer, aosp etc...) in your target."); + } + } return instance; } diff --git a/java/com/android/dialer/database/CallLogQueryHandler.java b/java/com/android/dialer/database/CallLogQueryHandler.java index ffca69f40..1f6bd5fb3 100644 --- a/java/com/android/dialer/database/CallLogQueryHandler.java +++ b/java/com/android/dialer/database/CallLogQueryHandler.java @@ -33,6 +33,7 @@ import android.os.Message; import android.provider.CallLog.Calls; import android.provider.VoicemailContract.Status; import android.provider.VoicemailContract.Voicemails; +import android.support.v4.os.BuildCompat; import com.android.contacts.common.database.NoNullCursorAsyncQueryHandler; import com.android.dialer.common.LogUtil; import com.android.dialer.compat.AppCompatConstants; @@ -40,6 +41,7 @@ import com.android.dialer.compat.SdkVersionOverride; import com.android.dialer.phonenumbercache.CallLogQuery; import com.android.dialer.telecom.TelecomUtil; import com.android.dialer.util.PermissionsUtil; +import com.android.voicemail.VoicemailComponent; import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.List; @@ -126,13 +128,23 @@ public class CallLogQueryHandler extends NoNullCursorAsyncQueryHandler { public void fetchVoicemailUnreadCount() { if (TelecomUtil.hasReadWriteVoicemailPermissions(mContext)) { // Only count voicemails that have not been read and have not been deleted. + StringBuilder where = + new StringBuilder(Voicemails.IS_READ + "=0" + " AND " + Voicemails.DELETED + "=0 "); + List selectionArgs = new ArrayList<>(); + + if (BuildCompat.isAtLeastO()) { + VoicemailComponent.get(mContext) + .getVoicemailClient() + .appendOmtpVoicemailSelectionClause(mContext, where, selectionArgs); + } + startQuery( QUERY_VOICEMAIL_UNREAD_COUNT_TOKEN, null, Voicemails.CONTENT_URI, new String[] {Voicemails._ID}, - Voicemails.IS_READ + "=0" + " AND " + Voicemails.DELETED + "=0", - null, + where.toString(), + selectionArgs.toArray(new String[selectionArgs.size()]), null); } } @@ -168,6 +180,12 @@ public class CallLogQueryHandler extends NoNullCursorAsyncQueryHandler { selectionArgs.add(Long.toString(newerThan)); } + if (callType == Calls.VOICEMAIL_TYPE) { + VoicemailComponent.get(mContext) + .getVoicemailClient() + .appendOmtpVoicemailSelectionClause(mContext, where, selectionArgs); + } + final int limit = (mLogLimit == -1) ? NUM_LOGS_TO_DISPLAY : mLogLimit; final String selection = where.length() > 0 ? where.toString() : null; Uri uri = diff --git a/java/com/android/dialer/debug/bindings/stub/DebugBindings.java b/java/com/android/dialer/debug/bindings/stub/DebugBindings.java new file mode 100644 index 000000000..7df38341d --- /dev/null +++ b/java/com/android/dialer/debug/bindings/stub/DebugBindings.java @@ -0,0 +1,27 @@ +/* + * 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.dialer.debug.bindings; + +import android.content.Context; + +/** Hooks into the debug module. */ +public class DebugBindings { + + public static void registerConnectionService(Context context) {} + + public static void addNewIncomingCall(Context context, String phoneNumber) {} +} diff --git a/java/com/android/dialer/enrichedcall/AutoValue_EnrichedCallCapabilities.java b/java/com/android/dialer/enrichedcall/AutoValue_EnrichedCallCapabilities.java deleted file mode 100644 index 14299f92c..000000000 --- a/java/com/android/dialer/enrichedcall/AutoValue_EnrichedCallCapabilities.java +++ /dev/null @@ -1,76 +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.dialer.enrichedcall; - -import javax.annotation.Generated; - -@Generated("com.google.auto.value.processor.AutoValueProcessor") - final class AutoValue_EnrichedCallCapabilities extends EnrichedCallCapabilities { - - private final boolean supportsCallComposer; - private final boolean supportsPostCall; - - AutoValue_EnrichedCallCapabilities( - boolean supportsCallComposer, - boolean supportsPostCall) { - this.supportsCallComposer = supportsCallComposer; - this.supportsPostCall = supportsPostCall; - } - - @Override - public boolean supportsCallComposer() { - return supportsCallComposer; - } - - @Override - public boolean supportsPostCall() { - return supportsPostCall; - } - - @Override - public String toString() { - return "EnrichedCallCapabilities{" - + "supportsCallComposer=" + supportsCallComposer + ", " - + "supportsPostCall=" + supportsPostCall + ", " - + "}"; - } - - @Override - public boolean equals(Object o) { - if (o == this) { - return true; - } - if (o instanceof EnrichedCallCapabilities) { - EnrichedCallCapabilities that = (EnrichedCallCapabilities) o; - return (this.supportsCallComposer == that.supportsCallComposer()) - && (this.supportsPostCall == that.supportsPostCall()); - } - return false; - } - - @Override - public int hashCode() { - int h = 1; - h *= 1000003; - h ^= this.supportsCallComposer ? 1231 : 1237; - h *= 1000003; - h ^= this.supportsPostCall ? 1231 : 1237; - return h; - } - -} - diff --git a/java/com/android/dialer/enrichedcall/AutoValue_OutgoingCallComposerData.java b/java/com/android/dialer/enrichedcall/AutoValue_OutgoingCallComposerData.java deleted file mode 100644 index edfefc479..000000000 --- a/java/com/android/dialer/enrichedcall/AutoValue_OutgoingCallComposerData.java +++ /dev/null @@ -1,127 +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.dialer.enrichedcall; - -import android.net.Uri; -import android.support.annotation.Nullable; -import javax.annotation.Generated; - -@Generated("com.google.auto.value.processor.AutoValueProcessor") - final class AutoValue_OutgoingCallComposerData extends OutgoingCallComposerData { - - private final String subject; - private final Uri imageUri; - private final String imageContentType; - - private AutoValue_OutgoingCallComposerData( - @Nullable String subject, - @Nullable Uri imageUri, - @Nullable String imageContentType) { - this.subject = subject; - this.imageUri = imageUri; - this.imageContentType = imageContentType; - } - - @Nullable - @Override - public String getSubject() { - return subject; - } - - @Nullable - @Override - public Uri getImageUri() { - return imageUri; - } - - @Nullable - @Override - public String getImageContentType() { - return imageContentType; - } - - @Override - public String toString() { - return "OutgoingCallComposerData{" - + "subject=" + subject + ", " - + "imageUri=" + imageUri + ", " - + "imageContentType=" + imageContentType - + "}"; - } - - @Override - public boolean equals(Object o) { - if (o == this) { - return true; - } - if (o instanceof OutgoingCallComposerData) { - OutgoingCallComposerData that = (OutgoingCallComposerData) o; - return ((this.subject == null) ? (that.getSubject() == null) : this.subject.equals(that.getSubject())) - && ((this.imageUri == null) ? (that.getImageUri() == null) : this.imageUri.equals(that.getImageUri())) - && ((this.imageContentType == null) ? (that.getImageContentType() == null) : this.imageContentType.equals(that.getImageContentType())); - } - return false; - } - - @Override - public int hashCode() { - int h = 1; - h *= 1000003; - h ^= (subject == null) ? 0 : this.subject.hashCode(); - h *= 1000003; - h ^= (imageUri == null) ? 0 : this.imageUri.hashCode(); - h *= 1000003; - h ^= (imageContentType == null) ? 0 : this.imageContentType.hashCode(); - return h; - } - - static final class Builder extends OutgoingCallComposerData.Builder { - private String subject; - private Uri imageUri; - private String imageContentType; - Builder() { - } - private Builder(OutgoingCallComposerData source) { - this.subject = source.getSubject(); - this.imageUri = source.getImageUri(); - this.imageContentType = source.getImageContentType(); - } - @Override - public OutgoingCallComposerData.Builder setSubject(@Nullable String subject) { - this.subject = subject; - return this; - } - @Override - OutgoingCallComposerData.Builder setImageUri(@Nullable Uri imageUri) { - this.imageUri = imageUri; - return this; - } - @Override - OutgoingCallComposerData.Builder setImageContentType(@Nullable String imageContentType) { - this.imageContentType = imageContentType; - return this; - } - @Override - OutgoingCallComposerData autoBuild() { - return new AutoValue_OutgoingCallComposerData( - this.subject, - this.imageUri, - this.imageContentType); - } - } - -} diff --git a/java/com/android/dialer/enrichedcall/EnrichedCallCapabilities.java b/java/com/android/dialer/enrichedcall/EnrichedCallCapabilities.java index b7d780950..c3c78c9c8 100644 --- a/java/com/android/dialer/enrichedcall/EnrichedCallCapabilities.java +++ b/java/com/android/dialer/enrichedcall/EnrichedCallCapabilities.java @@ -16,21 +16,24 @@ package com.android.dialer.enrichedcall; - +import com.google.auto.value.AutoValue; /** Value type holding enriched call capabilities. */ - +@AutoValue public abstract class EnrichedCallCapabilities { public static final EnrichedCallCapabilities NO_CAPABILITIES = - EnrichedCallCapabilities.create(false, false); + EnrichedCallCapabilities.create(false, false, false); public static EnrichedCallCapabilities create( - boolean supportsCallComposer, boolean supportsPostCall) { - return new AutoValue_EnrichedCallCapabilities(supportsCallComposer, supportsPostCall); + boolean supportsCallComposer, boolean supportsPostCall, boolean supportsVideoCall) { + return new AutoValue_EnrichedCallCapabilities( + supportsCallComposer, supportsPostCall, supportsVideoCall); } public abstract boolean supportsCallComposer(); public abstract boolean supportsPostCall(); + + public abstract boolean supportsVideoShare(); } diff --git a/java/com/android/dialer/enrichedcall/EnrichedCallComponent.java b/java/com/android/dialer/enrichedcall/EnrichedCallComponent.java new file mode 100644 index 000000000..5291e292f --- /dev/null +++ b/java/com/android/dialer/enrichedcall/EnrichedCallComponent.java @@ -0,0 +1,48 @@ +/* + * 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.enrichedcall; + +import android.content.Context; +import android.support.annotation.NonNull; +import dagger.Subcomponent; +import com.android.dialer.enrichedcall.stub.EnrichedCallManagerStub; + +/** Subcomponent that can be used to access the enriched call implementation. */ +public class EnrichedCallComponent { + private static EnrichedCallComponent instance; + private EnrichedCallManager enrichedCallManager; + + @NonNull + public EnrichedCallManager getEnrichedCallManager() { + if (enrichedCallManager == null) { + enrichedCallManager = new EnrichedCallManagerStub(); + } + return enrichedCallManager; + } + + public static EnrichedCallComponent get(Context context) { + if (instance == null) { + instance = new EnrichedCallComponent(); + } + return instance; + } + + /** Used to refer to the root application component. */ + public interface HasComponent { + EnrichedCallComponent enrichedCallComponent(); + } +} diff --git a/java/com/android/dialer/enrichedcall/EnrichedCallManager.java b/java/com/android/dialer/enrichedcall/EnrichedCallManager.java index 6af8c409a..a36b2cc0d 100644 --- a/java/com/android/dialer/enrichedcall/EnrichedCallManager.java +++ b/java/com/android/dialer/enrichedcall/EnrichedCallManager.java @@ -16,38 +16,25 @@ package com.android.dialer.enrichedcall; -import android.app.Application; import android.support.annotation.IntDef; import android.support.annotation.MainThread; import android.support.annotation.NonNull; import android.support.annotation.Nullable; -import com.android.dialer.common.Assert; +import android.support.annotation.WorkerThread; +import com.android.dialer.calldetails.nano.CallDetailsEntries; +import com.android.dialer.calldetails.nano.CallDetailsEntries.CallDetailsEntry; +import com.android.dialer.enrichedcall.historyquery.proto.nano.HistoryResult; +import com.android.dialer.enrichedcall.videoshare.VideoShareListener; import com.android.dialer.multimedia.MultimediaData; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; +import java.util.List; +import java.util.Map; /** Performs all enriched calling logic. */ public interface EnrichedCallManager { - /** Factory for {@link EnrichedCallManager}. */ - interface Factory { - EnrichedCallManager getEnrichedCallManager(); - } - - /** Accessor for {@link EnrichedCallManager}. */ - class Accessor { - - /** - * @throws IllegalArgumentException if application does not implement {@link - * EnrichedCallManager.Factory} - */ - @NonNull - public static EnrichedCallManager getInstance(@NonNull Application application) { - Assert.isNotNull(application); - - return ((EnrichedCallManager.Factory) application).getEnrichedCallManager(); - } - } + int POST_CALL_NOTE_MAX_CHAR = 60; /** Receives updates when enriched call capabilities are ready. */ interface CapabilitiesListener { @@ -147,6 +134,15 @@ public interface EnrichedCallManager { @MainThread void endCallComposerSession(long sessionId); + /** + * Sends a post call note to the given number. + * + * @throws IllegalArgumentException if message is longer than {@link #POST_CALL_NOTE_MAX_CHAR} + * characters + */ + @MainThread + void sendPostCallNote(@NonNull String number, @NonNull String message); + /** * Called once the capabilities are available for a corresponding call to {@link * #requestCapabilities(String)}. @@ -162,8 +158,8 @@ public interface EnrichedCallManager { interface StateChangedListener { /** - * Callback fired when state changes. Listeners should call {@link #getSession(String)} to - * retrieve the new state. + * Callback fired when state changes. Listeners should call {@link #getSession(long)} or {@link + * #getSession(String, String)} to retrieve the new state. */ void onEnrichedCallStateChanged(); } @@ -177,16 +173,28 @@ public interface EnrichedCallManager { @MainThread void registerStateChangedListener(@NonNull StateChangedListener listener); - /** Returns the {@link Session} for the given number, or {@code null} if no session exists. */ + /** Returns the {@link Session} for the given unique call id, falling back to the number. */ @MainThread @Nullable - Session getSession(@NonNull String number); + Session getSession(@NonNull String uniqueCallId, @NonNull String number); /** Returns the {@link Session} for the given sessionId, or {@code null} if no session exists. */ @MainThread @Nullable Session getSession(long sessionId); + /** + * Returns a mapping of enriched call data for all of the given {@link CallDetailsEntries}. + * + *

The mapping is created by finding the HistoryResults whose timestamps occurred during or + * close after a CallDetailsEntry. A CallDetailsEntry can have multiple HistoryResults in the + * event that both a CallComposer message and PostCall message were sent for the same call. + */ + @WorkerThread + @NonNull + Map> getAllHistoricalData( + @NonNull String number, @NonNull CallDetailsEntries entries); + /** * Unregisters the given {@link StateChangedListener}. * @@ -222,4 +230,77 @@ public interface EnrichedCallManager { */ @MainThread void onIncomingCallComposerData(long sessionId, @NonNull MultimediaData multimediaData); + + /** + * Called when post call data arrives for the given session. + * + * @throws IllegalStateException if there's no session for the given id + */ + @MainThread + void onIncomingPostCallData(long sessionId, @NonNull MultimediaData multimediaData); + + /** + * Registers the given {@link VideoShareListener}. + * + *

As a result of this method, the listener will receive updates when any video share state + * changes. + */ + @MainThread + void registerVideoShareListener(@NonNull VideoShareListener listener); + + /** + * Unregisters the given {@link VideoShareListener}. + * + *

As a result of this method, the listener will not receive updates when any video share state + * changes. + */ + @MainThread + void unregisterVideoShareListener(@NonNull VideoShareListener listener); + + /** Called when an incoming video share invite is received. */ + @MainThread + void onIncomingVideoShareInvite(long sessionId, @NonNull String number); + + /** + * Starts a video share session with the given remote number. + * + * @param number the remote number in any format + * @return the id for the started session, or {@link Session#NO_SESSION_ID} if the session fails + */ + @MainThread + long startVideoShareSession(@NonNull String number); + + /** + * Accepts a video share session invite. + * + * @param sessionId the session to accept + * @return whether or not accepting the session succeeded + */ + @MainThread + boolean acceptVideoShareSession(long sessionId); + + /** + * Retrieve the session id for an incoming video share invite. + * + * @param number the remote number in any format + * @return the id for the session invite, or {@link Session#NO_SESSION_ID} if there is no invite + */ + @MainThread + long getVideoShareInviteSessionId(@NonNull String number); + + /** + * Ends the given video share session. + * + * @param sessionId the id of the session to end + */ + @MainThread + void endVideoShareSession(long sessionId); + + /** + * Returns the {@link VideoShareSession} for the given sessionId, or {@code null} if no session + * exists. + */ + @MainThread + @Nullable + VideoShareSession getVideoShareSession(long sessionId); } diff --git a/java/com/android/dialer/enrichedcall/EnrichedCallManagerStub.java b/java/com/android/dialer/enrichedcall/EnrichedCallManagerStub.java deleted file mode 100644 index db9a799d3..000000000 --- a/java/com/android/dialer/enrichedcall/EnrichedCallManagerStub.java +++ /dev/null @@ -1,84 +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.dialer.enrichedcall; - -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import com.android.dialer.multimedia.MultimediaData; - -/** Stub implementation of {@link EnrichedCallManager}. */ -public final class EnrichedCallManagerStub implements EnrichedCallManager { - - @Override - public void registerCapabilitiesListener(@NonNull CapabilitiesListener listener) {} - - @Override - public void requestCapabilities(@NonNull String number) {} - - @Override - public void unregisterCapabilitiesListener(@NonNull CapabilitiesListener listener) {} - - @Override - public EnrichedCallCapabilities getCapabilities(@NonNull String number) { - return null; - } - - @Override - public void clearCachedData() {} - - @Override - public long startCallComposerSession(@NonNull String number) { - return Session.NO_SESSION_ID; - } - - @Override - public void sendCallComposerData(long sessionId, @NonNull MultimediaData data) {} - - @Override - public void endCallComposerSession(long sessionId) {} - - @Override - public void onCapabilitiesReceived( - @NonNull String number, @NonNull EnrichedCallCapabilities capabilities) {} - - @Override - public void registerStateChangedListener(@NonNull StateChangedListener listener) {} - - @Nullable - @Override - public Session getSession(@NonNull String number) { - return null; - } - - @Nullable - @Override - public Session getSession(long sessionId) { - return null; - } - - @Override - public void unregisterStateChangedListener(@NonNull StateChangedListener listener) {} - - @Override - public void onSessionStatusUpdate(long sessionId, @NonNull String number, int state) {} - - @Override - public void onMessageUpdate(long sessionId, @NonNull String messageId, int state) {} - - @Override - public void onIncomingCallComposerData(long sessionId, @NonNull MultimediaData multimediaData) {} -} diff --git a/java/com/android/dialer/enrichedcall/FuzzyPhoneNumberMatcher.java b/java/com/android/dialer/enrichedcall/FuzzyPhoneNumberMatcher.java new file mode 100644 index 000000000..f589f94a6 --- /dev/null +++ b/java/com/android/dialer/enrichedcall/FuzzyPhoneNumberMatcher.java @@ -0,0 +1,20 @@ +package com.android.dialer.enrichedcall; + +import android.support.annotation.NonNull; +import com.android.dialer.common.Assert; + +/** Utility for comparing phone numbers. */ +public class FuzzyPhoneNumberMatcher { + + /** Returns {@code true} if the given numbers can be interpreted to be the same. */ + public static boolean matches(@NonNull String a, @NonNull String b) { + String aNormalized = Assert.isNotNull(a).replaceAll("[^0-9]", ""); + String bNormalized = Assert.isNotNull(b).replaceAll("[^0-9]", ""); + if (aNormalized.length() < 7 || bNormalized.length() < 7) { + return false; + } + String aMatchable = aNormalized.substring(aNormalized.length() - 7); + String bMatchable = bNormalized.substring(bNormalized.length() - 7); + return aMatchable.equals(bMatchable); + } +} diff --git a/java/com/android/dialer/enrichedcall/OutgoingCallComposerData.java b/java/com/android/dialer/enrichedcall/OutgoingCallComposerData.java index a8ee49d4e..56145ddd4 100644 --- a/java/com/android/dialer/enrichedcall/OutgoingCallComposerData.java +++ b/java/com/android/dialer/enrichedcall/OutgoingCallComposerData.java @@ -20,7 +20,7 @@ import android.net.Uri; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import com.android.dialer.common.Assert; - +import com.google.auto.value.AutoValue; /** * Value type holding references to all data that could be provided for the call composer. @@ -29,19 +29,19 @@ import com.android.dialer.common.Assert; * *

  *   OutgoingCallComposerData.builder.build(); // throws exception, no data set
- *   OutgoingCallComposerData
- *       .setSubject(subject)
+ *   OutgoingCallComposerData.builder
+ *       .setText(subject)
  *       .build(); // Success
- *   OutgoingCallComposerData
+ *   OutgoingCallComposerData.builder
  *       .setImageData(uri, contentType)
  *       .build(); // Success
- *   OutgoingCallComposerData
- *      .setSubject(subject)
+ *   OutgoingCallComposerData.builder
+ *      .setText(subject)
  *      .setImageData(uri, contentType)
  *      .build(); // Success
  * 
*/ - +@AutoValue public abstract class OutgoingCallComposerData { public static Builder builder() { @@ -62,7 +62,7 @@ public abstract class OutgoingCallComposerData { public abstract String getImageContentType(); /** Builds instances of {@link OutgoingCallComposerData}. */ - + @AutoValue.Builder public abstract static class Builder { public abstract Builder setSubject(String subject); diff --git a/java/com/android/dialer/enrichedcall/Session.java b/java/com/android/dialer/enrichedcall/Session.java index b0439fae9..b3f291438 100644 --- a/java/com/android/dialer/enrichedcall/Session.java +++ b/java/com/android/dialer/enrichedcall/Session.java @@ -17,6 +17,7 @@ package com.android.dialer.enrichedcall; import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import com.android.dialer.enrichedcall.EnrichedCallManager.State; import com.android.dialer.multimedia.MultimediaData; @@ -38,6 +39,12 @@ public interface Session { */ long getSessionId(); + /** Returns the id of the dialer call associated with this session, or null if there isn't one. */ + @Nullable + String getUniqueDialerCallId(); + + void setUniqueDialerCallId(@NonNull String id); + /** Returns the number associated with the remote end of this session. */ @NonNull String getRemoteNumber(); diff --git a/java/com/android/dialer/enrichedcall/StubEnrichedCallModule.java b/java/com/android/dialer/enrichedcall/StubEnrichedCallModule.java deleted file mode 100644 index 39c55d040..000000000 --- a/java/com/android/dialer/enrichedcall/StubEnrichedCallModule.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * 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.enrichedcall; - -import dagger.Module; -import dagger.Provides; -import javax.inject.Singleton; - -/** Module which binds {@link EnrichedCallManagerStub}. */ -@Module -public class StubEnrichedCallModule { - - @Provides - @Singleton - static EnrichedCallManager provideEnrichedCallManager() { - return new EnrichedCallManagerStub(); - } -} diff --git a/java/com/android/dialer/enrichedcall/VideoShareSession.java b/java/com/android/dialer/enrichedcall/VideoShareSession.java new file mode 100644 index 000000000..07bc4ed09 --- /dev/null +++ b/java/com/android/dialer/enrichedcall/VideoShareSession.java @@ -0,0 +1,20 @@ +/* + * 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.enrichedcall; + +/** Holds state information and data about video share sessions. */ +public interface VideoShareSession {} diff --git a/java/com/android/dialer/enrichedcall/historyquery/HistoryQuery.java b/java/com/android/dialer/enrichedcall/historyquery/HistoryQuery.java new file mode 100644 index 000000000..b7593cebb --- /dev/null +++ b/java/com/android/dialer/enrichedcall/historyquery/HistoryQuery.java @@ -0,0 +1,31 @@ +package com.android.dialer.enrichedcall.historyquery; + +import android.support.annotation.NonNull; +import com.android.dialer.common.LogUtil; +import com.google.auto.value.AutoValue; + +/** + * Data object representing the pieces of information required to query for historical enriched call + * data. + */ +@AutoValue +public abstract class HistoryQuery { + + @NonNull + public static HistoryQuery create(@NonNull String number, long callStartTime, long callEndTime) { + return new AutoValue_HistoryQuery(number, callStartTime, callEndTime); + } + + public abstract String getNumber(); + + public abstract long getCallStartTimestamp(); + + public abstract long getCallEndTimestamp(); + + @Override + public String toString() { + return String.format( + "HistoryQuery{number: %s, callStartTimestamp: %d, callEndTimestamp: %d}", + LogUtil.sanitizePhoneNumber(getNumber()), getCallStartTimestamp(), getCallEndTimestamp()); + } +} diff --git a/java/com/android/dialer/enrichedcall/historyquery/nano/HistoryResult.java b/java/com/android/dialer/enrichedcall/historyquery/nano/HistoryResult.java new file mode 100644 index 000000000..2fdc2da50 --- /dev/null +++ b/java/com/android/dialer/enrichedcall/historyquery/nano/HistoryResult.java @@ -0,0 +1,203 @@ +/* + * 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. + */ + +// Generated by the protocol buffer compiler. DO NOT EDIT! + +package com.android.dialer.enrichedcall.historyquery.proto.nano; + +/** This file is autogenerated, but javadoc required. */ +@SuppressWarnings("hiding") +public final class HistoryResult + extends com.google.protobuf.nano.ExtendableMessageNano { + + /** This file is autogenerated, but javadoc required. */ + // enum Type + public interface Type { + public static final int INCOMING_CALL_COMPOSER = 1; + public static final int OUTGOING_CALL_COMPOSER = 2; + public static final int INCOMING_POST_CALL = 3; + public static final int OUTGOING_POST_CALL = 4; + } + + private static volatile HistoryResult[] _emptyArray; + + public static HistoryResult[] emptyArray() { + // Lazily initializes the empty array + if (_emptyArray == null) { + synchronized (com.google.protobuf.nano.InternalNano.LAZY_INIT_LOCK) { + if (_emptyArray == null) { + _emptyArray = new HistoryResult[0]; + } + } + } + return _emptyArray; + } + + // optional .com.android.dialer.enrichedcall.historyquery.proto.HistoryResult.Type type = 1; + public int type; + + // optional string text = 2; + public java.lang.String text; + + // optional string image_uri = 4; + public java.lang.String imageUri; + + // optional string image_content_type = 5; + public java.lang.String imageContentType; + + // optional int64 timestamp = 7; + public long timestamp; + + // @@protoc_insertion_point(class_scope:com.android.dialer.enrichedcall.historyquery.proto.HistoryResult) + + public HistoryResult() { + clear(); + } + + public HistoryResult clear() { + type = + com.android.dialer.enrichedcall.historyquery.proto.nano.HistoryResult.Type + .INCOMING_CALL_COMPOSER; + text = ""; + imageUri = ""; + imageContentType = ""; + timestamp = 0L; + unknownFieldData = null; + cachedSize = -1; + return this; + } + + @Override + public void writeTo(com.google.protobuf.nano.CodedOutputByteBufferNano output) + throws java.io.IOException { + if (this.type + != com.android.dialer.enrichedcall.historyquery.proto.nano.HistoryResult.Type + .INCOMING_CALL_COMPOSER) { + output.writeInt32(1, this.type); + } + if (this.text != null && !this.text.equals("")) { + output.writeString(2, this.text); + } + if (this.imageUri != null && !this.imageUri.equals("")) { + output.writeString(4, this.imageUri); + } + if (this.imageContentType != null && !this.imageContentType.equals("")) { + output.writeString(5, this.imageContentType); + } + if (this.timestamp != 0L) { + output.writeInt64(7, this.timestamp); + } + super.writeTo(output); + } + + @Override + protected int computeSerializedSize() { + int size = super.computeSerializedSize(); + if (this.type + != com.android.dialer.enrichedcall.historyquery.proto.nano.HistoryResult.Type + .INCOMING_CALL_COMPOSER) { + size += com.google.protobuf.nano.CodedOutputByteBufferNano.computeInt32Size(1, this.type); + } + if (this.text != null && !this.text.equals("")) { + size += com.google.protobuf.nano.CodedOutputByteBufferNano.computeStringSize(2, this.text); + } + if (this.imageUri != null && !this.imageUri.equals("")) { + size += + com.google.protobuf.nano.CodedOutputByteBufferNano.computeStringSize(4, this.imageUri); + } + if (this.imageContentType != null && !this.imageContentType.equals("")) { + size += + com.google.protobuf.nano.CodedOutputByteBufferNano.computeStringSize( + 5, this.imageContentType); + } + if (this.timestamp != 0L) { + size += + com.google.protobuf.nano.CodedOutputByteBufferNano.computeInt64Size(7, this.timestamp); + } + return size; + } + + @Override + public HistoryResult mergeFrom(com.google.protobuf.nano.CodedInputByteBufferNano input) + throws java.io.IOException { + while (true) { + int tag = input.readTag(); + switch (tag) { + case 0: + return this; + default: + { + if (!super.storeUnknownField(input, tag)) { + return this; + } + break; + } + case 8: + { + int initialPos = input.getPosition(); + int value = input.readInt32(); + switch (value) { + case com.android.dialer.enrichedcall.historyquery.proto.nano.HistoryResult.Type + .INCOMING_CALL_COMPOSER: + case com.android.dialer.enrichedcall.historyquery.proto.nano.HistoryResult.Type + .OUTGOING_CALL_COMPOSER: + case com.android.dialer.enrichedcall.historyquery.proto.nano.HistoryResult.Type + .INCOMING_POST_CALL: + case com.android.dialer.enrichedcall.historyquery.proto.nano.HistoryResult.Type + .OUTGOING_POST_CALL: + this.type = value; + break; + default: + input.rewindToPosition(initialPos); + storeUnknownField(input, tag); + break; + } + break; + } + case 18: + { + this.text = input.readString(); + break; + } + case 34: + { + this.imageUri = input.readString(); + break; + } + case 42: + { + this.imageContentType = input.readString(); + break; + } + case 56: + { + this.timestamp = input.readInt64(); + break; + } + } + } + } + + public static HistoryResult parseFrom(byte[] data) + throws com.google.protobuf.nano.InvalidProtocolBufferNanoException { + return com.google.protobuf.nano.MessageNano.mergeFrom(new HistoryResult(), data); + } + + public static HistoryResult parseFrom(com.google.protobuf.nano.CodedInputByteBufferNano input) + throws java.io.IOException { + return new HistoryResult().mergeFrom(input); + } +} diff --git a/java/com/android/dialer/enrichedcall/stub/EnrichedCallManagerStub.java b/java/com/android/dialer/enrichedcall/stub/EnrichedCallManagerStub.java new file mode 100644 index 000000000..01d1f2aac --- /dev/null +++ b/java/com/android/dialer/enrichedcall/stub/EnrichedCallManagerStub.java @@ -0,0 +1,145 @@ +/* + * 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.dialer.enrichedcall.stub; + +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.support.annotation.WorkerThread; +import android.util.ArrayMap; +import com.android.dialer.calldetails.nano.CallDetailsEntries; +import com.android.dialer.calldetails.nano.CallDetailsEntries.CallDetailsEntry; +import com.android.dialer.common.Assert; +import com.android.dialer.enrichedcall.EnrichedCallCapabilities; +import com.android.dialer.enrichedcall.EnrichedCallManager; +import com.android.dialer.enrichedcall.Session; +import com.android.dialer.enrichedcall.VideoShareSession; +import com.android.dialer.enrichedcall.historyquery.proto.nano.HistoryResult; +import com.android.dialer.enrichedcall.videoshare.VideoShareListener; +import com.android.dialer.multimedia.MultimediaData; +import java.util.List; +import java.util.Map; + +/** Stub implementation of {@link EnrichedCallManager}. */ +public final class EnrichedCallManagerStub implements EnrichedCallManager { + + @Override + public void registerCapabilitiesListener(@NonNull CapabilitiesListener listener) {} + + @Override + public void requestCapabilities(@NonNull String number) {} + + @Override + public void unregisterCapabilitiesListener(@NonNull CapabilitiesListener listener) {} + + @Override + public EnrichedCallCapabilities getCapabilities(@NonNull String number) { + return null; + } + + @Override + public void clearCachedData() {} + + @Override + public long startCallComposerSession(@NonNull String number) { + return Session.NO_SESSION_ID; + } + + @Override + public void sendCallComposerData(long sessionId, @NonNull MultimediaData data) {} + + @Override + public void endCallComposerSession(long sessionId) {} + + @Override + public void sendPostCallNote(@NonNull String number, @NonNull String message) {} + + @Override + public void onCapabilitiesReceived( + @NonNull String number, @NonNull EnrichedCallCapabilities capabilities) {} + + @Override + public void registerStateChangedListener(@NonNull StateChangedListener listener) {} + + @Nullable + @Override + public Session getSession(@NonNull String uniqueCallId, @NonNull String number) { + return null; + } + + @Nullable + @Override + public Session getSession(long sessionId) { + return null; + } + + @NonNull + @Override + @WorkerThread + public Map> getAllHistoricalData( + @NonNull String number, @NonNull CallDetailsEntries entries) { + Assert.isWorkerThread(); + return new ArrayMap<>(); + } + + @Override + public void unregisterStateChangedListener(@NonNull StateChangedListener listener) {} + + @Override + public void onSessionStatusUpdate(long sessionId, @NonNull String number, int state) {} + + @Override + public void onMessageUpdate(long sessionId, @NonNull String messageId, int state) {} + + @Override + public void onIncomingCallComposerData(long sessionId, @NonNull MultimediaData multimediaData) {} + + @Override + public void onIncomingPostCallData(long sessionId, @NonNull MultimediaData multimediaData) {} + + @Override + public void registerVideoShareListener(@NonNull VideoShareListener listener) {} + + @Override + public void unregisterVideoShareListener(@NonNull VideoShareListener listener) {} + + @Override + public void onIncomingVideoShareInvite(long sessionId, @NonNull String number) {} + + @Override + public long startVideoShareSession(String number) { + return Session.NO_SESSION_ID; + } + + @Override + public boolean acceptVideoShareSession(long sessionId) { + return false; + } + + @Override + public long getVideoShareInviteSessionId(@NonNull String number) { + return Session.NO_SESSION_ID; + } + + @Override + public void endVideoShareSession(long sessionId) {} + + @Nullable + @Override + public VideoShareSession getVideoShareSession(long sessionId) { + return null; + } +} diff --git a/java/com/android/dialer/enrichedcall/stub/StubEnrichedCallModule.java b/java/com/android/dialer/enrichedcall/stub/StubEnrichedCallModule.java new file mode 100644 index 000000000..0ec72111e --- /dev/null +++ b/java/com/android/dialer/enrichedcall/stub/StubEnrichedCallModule.java @@ -0,0 +1,35 @@ +/* + * 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.enrichedcall.stub; + +import com.android.dialer.enrichedcall.EnrichedCallManager; +import dagger.Module; +import dagger.Provides; +import javax.inject.Singleton; + +/** Module which binds {@link EnrichedCallManagerStub}. */ +@Module +public class StubEnrichedCallModule { + + @Provides + @Singleton + static EnrichedCallManager provideEnrichedCallManager() { + return new EnrichedCallManagerStub(); + } + + private StubEnrichedCallModule() {} +} diff --git a/java/com/android/dialer/enrichedcall/videoshare/VideoShareListener.java b/java/com/android/dialer/enrichedcall/videoshare/VideoShareListener.java new file mode 100644 index 000000000..bcc387a3f --- /dev/null +++ b/java/com/android/dialer/enrichedcall/videoshare/VideoShareListener.java @@ -0,0 +1,14 @@ +package com.android.dialer.enrichedcall.videoshare; + +import android.support.annotation.MainThread; + +/** Receives updates when video share status has changed. */ +public interface VideoShareListener { + + /** + * Callback fired when video share has changed (service connected / disconnected, video share + * invite received or canceled, or when a session changes). + */ + @MainThread + void onVideoShareChanged(); +} diff --git a/java/com/android/dialer/inject/ApplicationModule.java b/java/com/android/dialer/inject/ApplicationModule.java deleted file mode 100644 index 99e5296ea..000000000 --- a/java/com/android/dialer/inject/ApplicationModule.java +++ /dev/null @@ -1,39 +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.dialer.inject; - -import android.app.Application; -import android.support.annotation.NonNull; -import com.android.dialer.common.Assert; -import dagger.Module; -import dagger.Provides; - -/** Provides the singleton application object. */ -@Module -public final class ApplicationModule { - - @NonNull private final Application application; - - public ApplicationModule(@NonNull Application application) { - this.application = Assert.isNotNull(application); - } - - @Provides - Application provideApplication() { - return application; - } -} diff --git a/java/com/android/dialer/inject/ContextModule.java b/java/com/android/dialer/inject/ContextModule.java new file mode 100644 index 000000000..aa83f0105 --- /dev/null +++ b/java/com/android/dialer/inject/ContextModule.java @@ -0,0 +1,39 @@ +/* + * 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.inject; + +import android.content.Context; +import android.support.annotation.NonNull; +import com.android.dialer.common.Assert; +import dagger.Module; +import dagger.Provides; + +/** Provides the singleton context object. */ +@Module +public final class ContextModule { + + @NonNull private final Context context; + + public ContextModule(@NonNull Context context) { + this.context = Assert.isNotNull(context); + } + + @Provides + Context provideContext() { + return context; + } +} diff --git a/java/com/android/dialer/inject/DialerAppComponent.java b/java/com/android/dialer/inject/DialerAppComponent.java deleted file mode 100644 index 9832ce804..000000000 --- a/java/com/android/dialer/inject/DialerAppComponent.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * 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.inject; - -import com.android.dialer.enrichedcall.EnrichedCallManager; -import com.android.dialer.enrichedcall.StubEnrichedCallModule; -import dagger.Component; -import javax.inject.Singleton; - -/** Core application-wide {@link Component} for the open source dialer app. */ -@Singleton -@Component(modules = {ApplicationModule.class, StubEnrichedCallModule.class}) -public interface DialerAppComponent { - EnrichedCallManager enrichedCallManager(); -} diff --git a/java/com/android/dialer/inject/HasRootComponent.java b/java/com/android/dialer/inject/HasRootComponent.java new file mode 100644 index 000000000..0802b806a --- /dev/null +++ b/java/com/android/dialer/inject/HasRootComponent.java @@ -0,0 +1,25 @@ +/* + * 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.inject; + +/** + * Used by packages to access the root component from the Application without creating a dependency + * cycle. + */ +public interface HasRootComponent { + Object component(); +} diff --git a/java/com/android/dialer/interactions/PhoneNumberInteraction.java b/java/com/android/dialer/interactions/PhoneNumberInteraction.java index f36e5319c..b797629dc 100644 --- a/java/com/android/dialer/interactions/PhoneNumberInteraction.java +++ b/java/com/android/dialer/interactions/PhoneNumberInteraction.java @@ -81,8 +81,10 @@ public class PhoneNumberInteraction implements OnLoadCompleteListener { private static final String TAG = PhoneNumberInteraction.class.getSimpleName(); /** The identifier for a permissions request if one is generated. */ public static final int REQUEST_READ_CONTACTS = 1; + public static final int REQUEST_CALL_PHONE = 2; - private static final String[] PHONE_NUMBER_PROJECTION = + @VisibleForTesting + public static final String[] PHONE_NUMBER_PROJECTION = new String[] { Phone._ID, Phone.NUMBER, @@ -191,13 +193,14 @@ public class PhoneNumberInteraction implements OnLoadCompleteListener { * numbers have been queried for. The activity must implement {@link InteractionErrorListener} * and {@link DisambigDialogDismissedListener}. * @param isVideoCall {@code true} if the call is a video call, {@code false} otherwise. + * @return true if the necessary permissions were found to start the interaction, false otherwise */ - public static void startInteractionForPhoneCall( + public static boolean startInteractionForPhoneCall( TransactionSafeActivity activity, Uri uri, boolean isVideoCall, CallSpecificAppData callSpecificAppData) { - new PhoneNumberInteraction( + return new PhoneNumberInteraction( activity, ContactDisplayUtils.INTERACTION_CALL, isVideoCall, callSpecificAppData) .startInteraction(uri); } @@ -211,11 +214,19 @@ public class PhoneNumberInteraction implements OnLoadCompleteListener { * Initiates the interaction to result in either a phone call or sms message for a contact. * * @param uri Contact Uri + * @return true if the necessary permissions were found to start the interaction, false otherwise */ - private void startInteraction(Uri uri) { - // It's possible for a shortcut to have been created, and then Contacts permissions revoked. To - // avoid a crash when the user tries to use such a shortcut, check for this condition and ask - // the user for the permission. + private boolean startInteraction(Uri uri) { + // It's possible for a shortcut to have been created, and then permissions revoked. To avoid a + // crash when the user tries to use such a shortcut, check for this condition and ask the user + // for the permission. + if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.CALL_PHONE) + != PackageManager.PERMISSION_GRANTED) { + LogUtil.i("PhoneNumberInteraction.startInteraction", "No phone permissions"); + ActivityCompat.requestPermissions( + (Activity) mContext, new String[] {Manifest.permission.CALL_PHONE}, REQUEST_CALL_PHONE); + return false; + } if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) { LogUtil.i("PhoneNumberInteraction.startInteraction", "No contact permissions"); @@ -223,7 +234,7 @@ public class PhoneNumberInteraction implements OnLoadCompleteListener { (Activity) mContext, new String[] {Manifest.permission.READ_CONTACTS}, REQUEST_READ_CONTACTS); - return; + return false; } if (mLoader != null) { @@ -249,6 +260,7 @@ public class PhoneNumberInteraction implements OnLoadCompleteListener { mContext, queryUri, PHONE_NUMBER_PROJECTION, PHONE_NUMBER_SELECTION, null, null); mLoader.registerListener(0, this); mLoader.startLoading(); + return true; } @Override @@ -457,8 +469,8 @@ public class PhoneNumberInteraction implements OnLoadCompleteListener { * will be chosen to make a call or initiate an sms message. * *

It is recommended to use {@link #startInteractionForPhoneCall(TransactionSafeActivity, Uri, - * boolean, int)} instead of directly using this class, as those methods handle one or multiple - * data cases appropriately. + * boolean, CallSpecificAppData)} instead of directly using this class, as those methods handle + * one or multiple data cases appropriately. * *

This fragment may only be attached to activities which implement {@link * DisambigDialogDismissedListener}. diff --git a/java/com/android/dialer/logging/nano/ContactLookupResult.java b/java/com/android/dialer/logging/nano/ContactLookupResult.java index 8960560fb..93f5f0135 100644 --- a/java/com/android/dialer/logging/nano/ContactLookupResult.java +++ b/java/com/android/dialer/logging/nano/ContactLookupResult.java @@ -11,17 +11,19 @@ * 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 + * limitations under the License. */ // Generated by the protocol buffer compiler. DO NOT EDIT! package com.android.dialer.logging.nano; +/** This file is autogenerated, but javadoc required. */ @SuppressWarnings("hiding") -public final class ContactLookupResult extends - com.google.protobuf.nano.ExtendableMessageNano { +public final class ContactLookupResult + extends com.google.protobuf.nano.ExtendableMessageNano { + /** This file is autogenerated, but javadoc required. */ // enum Type public interface Type { public static final int UNKNOWN_LOOKUP_RESULT_TYPE = 0; @@ -34,11 +36,11 @@ public final class ContactLookupResult extends } private static volatile ContactLookupResult[] _emptyArray; + public static ContactLookupResult[] emptyArray() { // Lazily initializes the empty array if (_emptyArray == null) { - synchronized ( - com.google.protobuf.nano.InternalNano.LAZY_INIT_LOCK) { + synchronized (com.google.protobuf.nano.InternalNano.LAZY_INIT_LOCK) { if (_emptyArray == null) { _emptyArray = new ContactLookupResult[0]; } @@ -60,20 +62,20 @@ public final class ContactLookupResult extends } @Override - public ContactLookupResult mergeFrom( - com.google.protobuf.nano.CodedInputByteBufferNano input) + public ContactLookupResult mergeFrom(com.google.protobuf.nano.CodedInputByteBufferNano input) throws java.io.IOException { while (true) { int tag = input.readTag(); switch (tag) { case 0: return this; - default: { - if (!super.storeUnknownField(input, tag)) { - return this; + default: + { + if (!super.storeUnknownField(input, tag)) { + return this; + } + break; } - break; - } } } } @@ -84,8 +86,7 @@ public final class ContactLookupResult extends } public static ContactLookupResult parseFrom( - com.google.protobuf.nano.CodedInputByteBufferNano input) - throws java.io.IOException { + com.google.protobuf.nano.CodedInputByteBufferNano input) throws java.io.IOException { return new ContactLookupResult().mergeFrom(input); } } diff --git a/java/com/android/dialer/logging/nano/ContactSource.java b/java/com/android/dialer/logging/nano/ContactSource.java index 35d8b8ca1..dbe40cd53 100644 --- a/java/com/android/dialer/logging/nano/ContactSource.java +++ b/java/com/android/dialer/logging/nano/ContactSource.java @@ -11,17 +11,19 @@ * 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 + * limitations under the License. */ // Generated by the protocol buffer compiler. DO NOT EDIT! package com.android.dialer.logging.nano; +/** This file is autogenerated, but javadoc required. */ @SuppressWarnings("hiding") -public final class ContactSource extends - com.google.protobuf.nano.ExtendableMessageNano { +public final class ContactSource + extends com.google.protobuf.nano.ExtendableMessageNano { + /** This file is autogenerated, but javadoc required. */ // enum Type public interface Type { public static final int UNKNOWN_SOURCE_TYPE = 0; @@ -33,11 +35,11 @@ public final class ContactSource extends } private static volatile ContactSource[] _emptyArray; + public static ContactSource[] emptyArray() { // Lazily initializes the empty array if (_emptyArray == null) { - synchronized ( - com.google.protobuf.nano.InternalNano.LAZY_INIT_LOCK) { + synchronized (com.google.protobuf.nano.InternalNano.LAZY_INIT_LOCK) { if (_emptyArray == null) { _emptyArray = new ContactSource[0]; } @@ -59,20 +61,20 @@ public final class ContactSource extends } @Override - public ContactSource mergeFrom( - com.google.protobuf.nano.CodedInputByteBufferNano input) + public ContactSource mergeFrom(com.google.protobuf.nano.CodedInputByteBufferNano input) throws java.io.IOException { while (true) { int tag = input.readTag(); switch (tag) { case 0: return this; - default: { - if (!super.storeUnknownField(input, tag)) { - return this; + default: + { + if (!super.storeUnknownField(input, tag)) { + return this; + } + break; } - break; - } } } } @@ -82,8 +84,7 @@ public final class ContactSource extends return com.google.protobuf.nano.MessageNano.mergeFrom(new ContactSource(), data); } - public static ContactSource parseFrom( - com.google.protobuf.nano.CodedInputByteBufferNano input) + public static ContactSource parseFrom(com.google.protobuf.nano.CodedInputByteBufferNano input) throws java.io.IOException { return new ContactSource().mergeFrom(input); } diff --git a/java/com/android/dialer/logging/nano/DialerImpression.java b/java/com/android/dialer/logging/nano/DialerImpression.java index 6bb56751f..80a006b55 100644 --- a/java/com/android/dialer/logging/nano/DialerImpression.java +++ b/java/com/android/dialer/logging/nano/DialerImpression.java @@ -14,12 +14,16 @@ * limitations under the License. */ +// Generated by the protocol buffer compiler. DO NOT EDIT! + package com.android.dialer.logging.nano; +/** This file is autogenerated, but javadoc required. */ @SuppressWarnings("hiding") -public final class DialerImpression extends - com.google.protobuf.nano.ExtendableMessageNano { +public final class DialerImpression + extends com.google.protobuf.nano.ExtendableMessageNano { + /** This file is autogenerated, but javadoc required. */ // enum Type public interface Type { public static final int UNKNOWN_AOSP_EVENT_TYPE = 1000; @@ -33,7 +37,8 @@ public final class DialerImpression extends public static final int DIALOG_ACTION_CONFIRM_NUMBER_NOT_SPAM = 1008; public static final int REPORT_AS_NOT_SPAM_VIA_UNBLOCK_NUMBER = 1009; public static final int DIALOG_ACTION_CONFIRM_NUMBER_SPAM_INDIRECTLY_VIA_BLOCK_NUMBER = 1010; - public static final int REPORT_CALL_AS_SPAM_VIA_CALL_LOG_BLOCK_REPORT_SPAM_SENT_VIA_BLOCK_NUMBER_DIALOG = 1011; + public static final int + REPORT_CALL_AS_SPAM_VIA_CALL_LOG_BLOCK_REPORT_SPAM_SENT_VIA_BLOCK_NUMBER_DIALOG = 1011; public static final int USER_ACTION_BLOCKED_NUMBER = 1012; public static final int USER_ACTION_UNBLOCKED_NUMBER = 1013; public static final int SPAM_AFTER_CALL_NOTIFICATION_BLOCK_NUMBER = 1014; @@ -41,7 +46,8 @@ public final class DialerImpression extends public static final int SPAM_AFTER_CALL_NOTIFICATION_SHOW_NON_SPAM_DIALOG = 1016; public static final int SPAM_AFTER_CALL_NOTIFICATION_ADD_TO_CONTACTS = 1019; public static final int SPAM_AFTER_CALL_NOTIFICATION_MARKED_NUMBER_AS_SPAM = 1020; - public static final int SPAM_AFTER_CALL_NOTIFICATION_MARKED_NUMBER_AS_NOT_SPAM_AND_BLOCKED = 1021; + public static final int SPAM_AFTER_CALL_NOTIFICATION_MARKED_NUMBER_AS_NOT_SPAM_AND_BLOCKED = + 1021; public static final int SPAM_AFTER_CALL_NOTIFICATION_REPORT_NUMBER_AS_NOT_SPAM = 1022; public static final int SPAM_AFTER_CALL_NOTIFICATION_ON_DISMISS_SPAM_DIALOG = 1024; public static final int SPAM_AFTER_CALL_NOTIFICATION_ON_DISMISS_NON_SPAM_DIALOG = 1025; @@ -89,7 +95,8 @@ public final class DialerImpression extends public static final int STORAGE_PERMISSION_DENIED = 1073; public static final int CAMERA_PERMISSION_DENIED = 1078; public static final int VOICEMAIL_CONFIGURATION_STATE_CORRUPTION_DETECTED_FROM_ACTIVITY = 1079; - public static final int VOICEMAIL_CONFIGURATION_STATE_CORRUPTION_DETECTED_FROM_NOTIFICATION = 1080; + public static final int VOICEMAIL_CONFIGURATION_STATE_CORRUPTION_DETECTED_FROM_NOTIFICATION = + 1080; public static final int BACKUP_ON_BACKUP = 1081; public static final int BACKUP_ON_FULL_BACKUP = 1082; public static final int BACKUP_ON_BACKUP_DISABLED = 1083; @@ -116,15 +123,43 @@ public final class DialerImpression extends public static final int BACKUP_ON_RESTORE_VM_DUPLICATE_NOT_RESTORING = 1104; public static final int CALL_LOG_SHARE_AND_CALL = 1105; public static final int CALL_COMPOSER_ACTIVITY_PLACE_RCS_CALL = 1106; - public static final int CALL_COMPOSER_ACTIVITY_SEND_AND_CALL_PRESSED_WHEN_SESSION_NOT_READY = 1107; + public static final int CALL_COMPOSER_ACTIVITY_SEND_AND_CALL_PRESSED_WHEN_SESSION_NOT_READY = + 1107; + public static final int POST_CALL_PROMPT_USER_TO_SEND_MESSAGE_CLICKED = 1108; + public static final int POST_CALL_PROMPT_USER_TO_SEND_MESSAGE = 1109; + public static final int POST_CALL_PROMPT_USER_TO_VIEW_SENT_MESSAGE = 1110; + public static final int POST_CALL_PROMPT_USER_TO_VIEW_SENT_MESSAGE_CLICKED = 1111; + public static final int IN_CALL_SCREEN_TURN_ON_MUTE = 1112; + public static final int IN_CALL_SCREEN_TURN_OFF_MUTE = 1113; + public static final int IN_CALL_SCREEN_SWAP_CAMERA = 1114; + public static final int IN_CALL_SCREEN_TURN_ON_VIDEO = 1115; + public static final int IN_CALL_SCREEN_TURN_OFF_VIDEO = 1116; + public static final int VIDEO_CALL_WITH_INCOMING_VOICE_CALL = 1117; + public static final int VIDEO_CALL_WITH_INCOMING_VIDEO_CALL = 1118; + public static final int VOICE_CALL_WITH_INCOMING_VOICE_CALL = 1119; + public static final int VOICE_CALL_WITH_INCOMING_VIDEO_CALL = 1120; + public static final int CALL_DETAILS_COPY_NUMBER = 1121; + public static final int CALL_DETAILS_EDIT_BEFORE_CALL = 1122; + public static final int CALL_DETAILS_CALL_BACK = 1123; + public static final int VVM_USER_DISMISSED_VM_ALMOST_FULL_PROMO = 1124; + public static final int VVM_USER_DISMISSED_VM_FULL_PROMO = 1125; + public static final int VVM_USER_ENABLED_ARCHIVE_FROM_VM_ALMOST_FULL_PROMO = 1126; + public static final int VVM_USER_ENABLED_ARCHIVE_FROM_VM_FULL_PROMO = 1127; + public static final int VVM_USER_SHOWN_VM_ALMOST_FULL_PROMO = 1128; + public static final int VVM_USER_SHOWN_VM_FULL_PROMO = 1129; + public static final int VVM_USER_SHOWN_VM_ALMOST_FULL_ERROR_MESSAGE = 1130; + public static final int VVM_USER_SHOWN_VM_FULL_ERROR_MESSAGE = 1131; + public static final int VVM_USER_TURNED_ARCHIVE_ON_FROM_SETTINGS = 1132; + public static final int VVM_USER_TURNED_ARCHIVE_OFF_FROM_SETTINGS = 1133; + public static final int VVM_ARCHIVE_AUTO_DELETED_VM_FROM_SERVER = 1134; + public static final int VVM_ARCHIVE_AUTO_DELETE_TURNED_OFF = 1135; } private static volatile DialerImpression[] _emptyArray; public static DialerImpression[] emptyArray() { // Lazily initializes the empty array if (_emptyArray == null) { - synchronized ( - com.google.protobuf.nano.InternalNano.LAZY_INIT_LOCK) { + synchronized (com.google.protobuf.nano.InternalNano.LAZY_INIT_LOCK) { if (_emptyArray == null) { _emptyArray = new DialerImpression[0]; } @@ -146,20 +181,20 @@ public final class DialerImpression extends } @Override - public DialerImpression mergeFrom( - com.google.protobuf.nano.CodedInputByteBufferNano input) + public DialerImpression mergeFrom(com.google.protobuf.nano.CodedInputByteBufferNano input) throws java.io.IOException { while (true) { int tag = input.readTag(); switch (tag) { case 0: return this; - default: { - if (!super.storeUnknownField(input, tag)) { - return this; + default: + { + if (!super.storeUnknownField(input, tag)) { + return this; + } + break; } - break; - } } } } @@ -169,10 +204,8 @@ public final class DialerImpression extends return com.google.protobuf.nano.MessageNano.mergeFrom(new DialerImpression(), data); } - public static DialerImpression parseFrom( - com.google.protobuf.nano.CodedInputByteBufferNano input) + public static DialerImpression parseFrom(com.google.protobuf.nano.CodedInputByteBufferNano input) throws java.io.IOException { return new DialerImpression().mergeFrom(input); } } - diff --git a/java/com/android/dialer/logging/nano/InteractionEvent.java b/java/com/android/dialer/logging/nano/InteractionEvent.java index 8d9430be9..7ca95fa45 100644 --- a/java/com/android/dialer/logging/nano/InteractionEvent.java +++ b/java/com/android/dialer/logging/nano/InteractionEvent.java @@ -23,8 +23,8 @@ package com.android.dialer.logging.nano; public final class InteractionEvent extends com.google.protobuf.nano.ExtendableMessageNano { - // enum Type /** This file is autogenerated, but javadoc required. */ + // enum Type public interface Type { public static final int UNKNOWN = 0; public static final int CALL_BLOCKED = 15; diff --git a/java/com/android/dialer/logging/nano/ReportingLocation.java b/java/com/android/dialer/logging/nano/ReportingLocation.java index 1f05ce414..08ee04e7e 100644 --- a/java/com/android/dialer/logging/nano/ReportingLocation.java +++ b/java/com/android/dialer/logging/nano/ReportingLocation.java @@ -11,17 +11,19 @@ * 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 + * limitations under the License. */ // Generated by the protocol buffer compiler. DO NOT EDIT! package com.android.dialer.logging.nano; +/** This file is autogenerated, but javadoc required. */ @SuppressWarnings("hiding") -public final class ReportingLocation extends - com.google.protobuf.nano.ExtendableMessageNano { +public final class ReportingLocation + extends com.google.protobuf.nano.ExtendableMessageNano { + /** This file is autogenerated, but javadoc required. */ // enum Type public interface Type { public static final int UNKNOWN_REPORTING_LOCATION = 0; @@ -30,11 +32,11 @@ public final class ReportingLocation extends } private static volatile ReportingLocation[] _emptyArray; + public static ReportingLocation[] emptyArray() { // Lazily initializes the empty array if (_emptyArray == null) { - synchronized ( - com.google.protobuf.nano.InternalNano.LAZY_INIT_LOCK) { + synchronized (com.google.protobuf.nano.InternalNano.LAZY_INIT_LOCK) { if (_emptyArray == null) { _emptyArray = new ReportingLocation[0]; } @@ -56,20 +58,20 @@ public final class ReportingLocation extends } @Override - public ReportingLocation mergeFrom( - com.google.protobuf.nano.CodedInputByteBufferNano input) + public ReportingLocation mergeFrom(com.google.protobuf.nano.CodedInputByteBufferNano input) throws java.io.IOException { while (true) { int tag = input.readTag(); switch (tag) { case 0: return this; - default: { - if (!super.storeUnknownField(input, tag)) { - return this; + default: + { + if (!super.storeUnknownField(input, tag)) { + return this; + } + break; } - break; - } } } } @@ -79,8 +81,7 @@ public final class ReportingLocation extends return com.google.protobuf.nano.MessageNano.mergeFrom(new ReportingLocation(), data); } - public static ReportingLocation parseFrom( - com.google.protobuf.nano.CodedInputByteBufferNano input) + public static ReportingLocation parseFrom(com.google.protobuf.nano.CodedInputByteBufferNano input) throws java.io.IOException { return new ReportingLocation().mergeFrom(input); } diff --git a/java/com/android/dialer/logging/nano/ScreenEvent.java b/java/com/android/dialer/logging/nano/ScreenEvent.java index be4e5eb9e..bd5b817e1 100644 --- a/java/com/android/dialer/logging/nano/ScreenEvent.java +++ b/java/com/android/dialer/logging/nano/ScreenEvent.java @@ -22,8 +22,8 @@ package com.android.dialer.logging.nano; @SuppressWarnings("hiding") public final class ScreenEvent extends com.google.protobuf.nano.ExtendableMessageNano { - // enum Type /** This file is autogenerated, but javadoc required. */ + // enum Type public interface Type { public static final int UNKNOWN = 0; public static final int DIALPAD = 1; diff --git a/java/com/android/dialer/multimedia/AutoValue_MultimediaData.java b/java/com/android/dialer/multimedia/AutoValue_MultimediaData.java deleted file mode 100644 index cc6815094..000000000 --- a/java/com/android/dialer/multimedia/AutoValue_MultimediaData.java +++ /dev/null @@ -1,165 +0,0 @@ -/* - * 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.multimedia; - -import android.location.Location; -import android.net.Uri; -import android.support.annotation.Nullable; -import javax.annotation.Generated; - -@Generated("com.google.auto.value.processor.AutoValueProcessor") - final class AutoValue_MultimediaData extends MultimediaData { - - private final String subject; - private final Location location; - private final Uri imageUri; - private final String imageContentType; - private final boolean important; - - private AutoValue_MultimediaData( - @Nullable String subject, - @Nullable Location location, - @Nullable Uri imageUri, - @Nullable String imageContentType, - boolean important) { - this.subject = subject; - this.location = location; - this.imageUri = imageUri; - this.imageContentType = imageContentType; - this.important = important; - } - - @Nullable - @Override - public String getSubject() { - return subject; - } - - @Nullable - @Override - public Location getLocation() { - return location; - } - - @Nullable - @Override - public Uri getImageUri() { - return imageUri; - } - - @Nullable - @Override - public String getImageContentType() { - return imageContentType; - } - - @Override - public boolean isImportant() { - return important; - } - - @Override - public boolean equals(Object o) { - if (o == this) { - return true; - } - if (o instanceof MultimediaData) { - MultimediaData that = (MultimediaData) o; - return ((this.subject == null) ? (that.getSubject() == null) : this.subject.equals(that.getSubject())) - && ((this.location == null) ? (that.getLocation() == null) : this.location.equals(that.getLocation())) - && ((this.imageUri == null) ? (that.getImageUri() == null) : this.imageUri.equals(that.getImageUri())) - && ((this.imageContentType == null) ? (that.getImageContentType() == null) : this.imageContentType.equals(that.getImageContentType())) - && (this.important == that.isImportant()); - } - return false; - } - - @Override - public int hashCode() { - int h = 1; - h *= 1000003; - h ^= (subject == null) ? 0 : this.subject.hashCode(); - h *= 1000003; - h ^= (location == null) ? 0 : this.location.hashCode(); - h *= 1000003; - h ^= (imageUri == null) ? 0 : this.imageUri.hashCode(); - h *= 1000003; - h ^= (imageContentType == null) ? 0 : this.imageContentType.hashCode(); - h *= 1000003; - h ^= this.important ? 1231 : 1237; - return h; - } - - static final class Builder extends MultimediaData.Builder { - private String subject; - private Location location; - private Uri imageUri; - private String imageContentType; - private Boolean important; - Builder() { - } - private Builder(MultimediaData source) { - this.subject = source.getSubject(); - this.location = source.getLocation(); - this.imageUri = source.getImageUri(); - this.imageContentType = source.getImageContentType(); - this.important = source.isImportant(); - } - @Override - public MultimediaData.Builder setSubject(@Nullable String subject) { - this.subject = subject; - return this; - } - @Override - public MultimediaData.Builder setLocation(@Nullable Location location) { - this.location = location; - return this; - } - @Override - MultimediaData.Builder setImageUri(@Nullable Uri imageUri) { - this.imageUri = imageUri; - return this; - } - @Override - MultimediaData.Builder setImageContentType(@Nullable String imageContentType) { - this.imageContentType = imageContentType; - return this; - } - @Override - public MultimediaData.Builder setImportant(boolean important) { - this.important = important; - return this; - } - @Override - public MultimediaData build() { - String missing = ""; - if (this.important == null) { - missing += " important"; - } - if (!missing.isEmpty()) { - throw new IllegalStateException("Missing required properties:" + missing); - } - return new AutoValue_MultimediaData( - this.subject, - this.location, - this.imageUri, - this.imageContentType, - this.important); - } - } - -} diff --git a/java/com/android/dialer/multimedia/MultimediaData.java b/java/com/android/dialer/multimedia/MultimediaData.java index ebd41a918..22bb7641c 100644 --- a/java/com/android/dialer/multimedia/MultimediaData.java +++ b/java/com/android/dialer/multimedia/MultimediaData.java @@ -21,10 +21,10 @@ import android.net.Uri; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import com.android.dialer.common.LogUtil; +import com.google.auto.value.AutoValue; - -/** Holds the data associated with an enriched call session. */ - +/** Holds data associated with a call. */ +@AutoValue public abstract class MultimediaData { public static final MultimediaData EMPTY = builder().build(); @@ -34,32 +34,33 @@ public abstract class MultimediaData { return new AutoValue_MultimediaData.Builder().setImportant(false); } - /** Returns the call composer subject if set, or null if this isn't a call composer session. */ + /** + * Returns the text part of this data. + * + *

This field is used for both the call composer session and the post call note. + */ @Nullable - public abstract String getSubject(); + public abstract String getText(); - /** Returns the call composer location if set, or null if this isn't a call composer session. */ + /** Returns the location part of this data. */ @Nullable public abstract Location getLocation(); - /** Returns {@code true} if this session contains image data. */ + /** Returns {@code true} if this object contains image data. */ public boolean hasImageData() { // imageUri and content are always either both null or nonnull return getImageUri() != null && getImageContentType() != null; } - /** Returns the call composer photo if set, or null if this isn't a call composer session. */ + /** Returns the image uri part of this object's image. */ @Nullable public abstract Uri getImageUri(); - /** - * Returns the content type of the image, either image/png or image/jpeg, if set, or null if this - * isn't a call composer session. - */ + /** Returns the content type part of this object's image, either image/png or image/jpeg. */ @Nullable public abstract String getImageContentType(); - /** Returns {@code true} if this is a call composer session that's marked as important. */ + /** Returns {@code true} if this data is marked as important. */ public abstract boolean isImportant(); /** Returns the string form of this MultimediaData with no PII. */ @@ -68,7 +69,7 @@ public abstract class MultimediaData { return String.format( "MultimediaData{subject: %s, location: %s, imageUrl: %s, imageContentType: %s, " + "important: %b}", - LogUtil.sanitizePii(getSubject()), + LogUtil.sanitizePii(getText()), LogUtil.sanitizePii(getLocation()), LogUtil.sanitizePii(getImageUri()), getImageContentType(), @@ -76,10 +77,10 @@ public abstract class MultimediaData { } /** Creates instances of {@link MultimediaData}. */ - + @AutoValue.Builder public abstract static class Builder { - public abstract Builder setSubject(@NonNull String subject); + public abstract Builder setText(@NonNull String subject); public abstract Builder setLocation(@NonNull Location location); diff --git a/java/com/android/dialer/notification/AndroidManifest.xml b/java/com/android/dialer/notification/AndroidManifest.xml new file mode 100644 index 000000000..c5484f263 --- /dev/null +++ b/java/com/android/dialer/notification/AndroidManifest.xml @@ -0,0 +1,21 @@ + + + + + diff --git a/java/com/android/dialer/notification/GroupedNotificationUtil.java b/java/com/android/dialer/notification/GroupedNotificationUtil.java new file mode 100644 index 000000000..63ea51739 --- /dev/null +++ b/java/com/android/dialer/notification/GroupedNotificationUtil.java @@ -0,0 +1,66 @@ +/* + * 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.notification; + +import android.app.NotificationManager; +import android.service.notification.StatusBarNotification; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import java.util.Objects; + +/** Utilities for dealing with grouped notifications */ +public final class GroupedNotificationUtil { + + /** + * Remove notification(s) that were added as part of a group. Will ensure that if this is the last + * notification in the group the summary will be removed. + * + * @param tag String tag as included in {@link NotificationManager#notify(String, int, + * android.app.Notification)}. If null will remove all notifications under id + * @param id notification id as included with {@link NotificationManager#notify(String, int, + * android.app.Notification)}. + * @param summaryTag String tag of the summary notification + */ + public static void removeNotification( + @NonNull NotificationManager notificationManager, + @Nullable String tag, + int id, + @NonNull String summaryTag) { + if (tag == null) { + // Clear all missed call notifications + for (StatusBarNotification notification : notificationManager.getActiveNotifications()) { + if (notification.getId() == id) { + notificationManager.cancel(notification.getTag(), id); + } + } + } else { + notificationManager.cancel(tag, id); + + // See if other non-summary missed call notifications exist, and if not then clear the summary + boolean clearSummary = true; + for (StatusBarNotification notification : notificationManager.getActiveNotifications()) { + if (notification.getId() == id && !Objects.equals(summaryTag, notification.getTag())) { + clearSummary = false; + break; + } + } + if (clearSummary) { + notificationManager.cancel(summaryTag, id); + } + } + } +} diff --git a/java/com/android/dialer/notification/NotificationChannelManager.java b/java/com/android/dialer/notification/NotificationChannelManager.java new file mode 100644 index 000000000..9ff57321e --- /dev/null +++ b/java/com/android/dialer/notification/NotificationChannelManager.java @@ -0,0 +1,232 @@ +/* + * 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.notification; + +import android.app.Notification; +import android.app.NotificationChannel; +import android.app.NotificationChannelGroup; +import android.app.NotificationManager; +import android.content.Context; +import android.media.AudioAttributes; +import android.net.Uri; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.support.annotation.StringDef; +import android.support.v4.os.BuildCompat; +import android.telecom.PhoneAccount; +import android.telecom.PhoneAccountHandle; +import android.telecom.TelecomManager; +import android.telephony.TelephonyManager; +import com.android.contacts.common.compat.TelephonyManagerCompat; +import com.android.dialer.buildtype.BuildType; +import com.android.dialer.common.LogUtil; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +/** Contains info on how to create {@link NotificationChannel NotificationChannels} */ +public class NotificationChannelManager { + + private static NotificationChannelManager instance; + + public static NotificationChannelManager getInstance() { + if (instance == null) { + instance = new NotificationChannelManager(); + } + return instance; + } + + /** + * Set the channel of notification appropriately. Will create the channel if it does not already + * exist. Safe to call pre-O (will no-op). + * + *

phoneAccount should only be null if channelName is {@link Channel#MISC}. + */ + public static void applyChannel( + @NonNull Notification.Builder notification, + @NonNull Context context, + @Channel String channelName, + @Nullable PhoneAccountHandle phoneAccount) { + if (phoneAccount == null) { + if (!Channel.MISC.equals(channelName)) { + IllegalArgumentException exception = + new IllegalArgumentException( + "Phone account handle must not be null unless on Channel.MISC"); + if (BuildType.get() >= BuildType.RELEASE) { + LogUtil.e("NotificationChannelManager.applyChannel", null, exception); + } else { + throw exception; + } + } + } + + if (BuildCompat.isAtLeastO()) { + NotificationChannel channel = + NotificationChannelManager.getInstance().getChannel(context, channelName, phoneAccount); + notification.setChannel(channel.getId()); + } + } + + /** The base Channel IDs for {@link NotificationChannel} */ + @Retention(RetentionPolicy.SOURCE) + @StringDef({ + Channel.INCOMING_CALL, + Channel.ONGOING_CALL, + Channel.MISSED_CALL, + Channel.VOICEMAIL, + Channel.EXTERNAL_CALL, + Channel.MISC + }) + public @interface Channel { + String INCOMING_CALL = "incomingCall"; + String ONGOING_CALL = "ongoingCall"; + String MISSED_CALL = "missedCall"; + String VOICEMAIL = "voicemail"; + String EXTERNAL_CALL = "externalCall"; + String MISC = "miscellaneous"; + } + + private NotificationChannelManager() {} + + private NotificationChannel getChannel( + @NonNull Context context, + @Channel String channelName, + @Nullable PhoneAccountHandle phoneAccount) { + String channelId = channelNameToId(channelName, phoneAccount); + NotificationChannel channel = getNotificationManager(context).getNotificationChannel(channelId); + if (channel == null) { + channel = createChannel(context, channelName, phoneAccount); + } + return channel; + } + + private static String channelNameToId( + @Channel String name, @Nullable PhoneAccountHandle phoneAccountHandle) { + if (phoneAccountHandle == null) { + return name; + } else { + return name + ":" + phoneAccountHandle.getId(); + } + } + + private NotificationChannel createChannel( + Context context, + @Channel String channelName, + @Nullable PhoneAccountHandle phoneAccountHandle) { + String channelId = channelNameToId(channelName, phoneAccountHandle); + + if (phoneAccountHandle != null) { + PhoneAccount account = getTelecomManager(context).getPhoneAccount(phoneAccountHandle); + NotificationChannelGroup group = + new NotificationChannelGroup( + phoneAccountHandle.getId(), + (account == null) ? phoneAccountHandle.getId() : account.getLabel().toString()); + getNotificationManager(context) + .createNotificationChannelGroup(group); // No-op if already exists + } else if (!Channel.MISC.equals(channelName)) { + LogUtil.w( + "NotificationChannelManager.createChannel", + "Null PhoneAccountHandle with channel " + channelName); + } + + Uri silentRingtone = Uri.parse(""); + + CharSequence name; + int importance; + boolean canShowBadge; + boolean lights; + boolean vibration; + Uri sound; + switch (channelName) { + case Channel.INCOMING_CALL: + name = context.getText(R.string.notification_channel_incoming_call); + importance = NotificationManager.IMPORTANCE_MAX; + canShowBadge = false; + lights = true; + vibration = false; + sound = silentRingtone; + break; + case Channel.MISSED_CALL: + name = context.getText(R.string.notification_channel_missed_call); + importance = NotificationManager.IMPORTANCE_DEFAULT; + canShowBadge = true; + lights = true; + vibration = true; + sound = silentRingtone; + break; + case Channel.ONGOING_CALL: + name = context.getText(R.string.notification_channel_ongoing_call); + importance = NotificationManager.IMPORTANCE_DEFAULT; + canShowBadge = false; + lights = false; + vibration = false; + sound = null; + break; + case Channel.VOICEMAIL: + name = context.getText(R.string.notification_channel_voicemail); + importance = NotificationManager.IMPORTANCE_DEFAULT; + canShowBadge = true; + lights = true; + vibration = true; + sound = + TelephonyManagerCompat.getVoicemailRingtoneUri( + getTelephonyManager(context), phoneAccountHandle); + break; + case Channel.EXTERNAL_CALL: + name = context.getText(R.string.notification_channel_external_call); + importance = NotificationManager.IMPORTANCE_HIGH; + canShowBadge = false; + lights = true; + vibration = true; + sound = null; + break; + case Channel.MISC: + name = context.getText(R.string.notification_channel_misc); + importance = NotificationManager.IMPORTANCE_DEFAULT; + canShowBadge = false; + lights = true; + vibration = true; + sound = null; + break; + default: + throw new IllegalArgumentException("Unknown channel: " + channelName); + } + + NotificationChannel channel = new NotificationChannel(channelId, name, importance); + channel.setShowBadge(canShowBadge); + if (sound != null) { + channel.setSound( + sound, + new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION).build()); + } + channel.enableLights(lights); + channel.enableVibration(vibration); + getNotificationManager(context).createNotificationChannel(channel); + return channel; + } + + private static NotificationManager getNotificationManager(@NonNull Context context) { + return context.getSystemService(NotificationManager.class); + } + + private static TelephonyManager getTelephonyManager(@NonNull Context context) { + return context.getSystemService(TelephonyManager.class); + } + + private static TelecomManager getTelecomManager(@NonNull Context context) { + return context.getSystemService(TelecomManager.class); + } +} diff --git a/java/com/android/dialer/notification/res/values/ids.xml b/java/com/android/dialer/notification/res/values/ids.xml new file mode 100644 index 000000000..6bdb489a7 --- /dev/null +++ b/java/com/android/dialer/notification/res/values/ids.xml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + diff --git a/java/com/android/dialer/notification/res/values/strings.xml b/java/com/android/dialer/notification/res/values/strings.xml new file mode 100644 index 000000000..2fc4962c6 --- /dev/null +++ b/java/com/android/dialer/notification/res/values/strings.xml @@ -0,0 +1,25 @@ + + + + + Incoming calls + Ongoing calls + Missed calls + Voicemails + External calls + Miscellaneous + diff --git a/java/com/android/dialer/oem/AndroidManifest.xml b/java/com/android/dialer/oem/AndroidManifest.xml new file mode 100644 index 000000000..e161a6d14 --- /dev/null +++ b/java/com/android/dialer/oem/AndroidManifest.xml @@ -0,0 +1,3 @@ + + \ No newline at end of file diff --git a/java/com/android/dialer/oem/MotorolaHiddenMenuKeySequence.java b/java/com/android/dialer/oem/MotorolaHiddenMenuKeySequence.java new file mode 100644 index 000000000..18f621e01 --- /dev/null +++ b/java/com/android/dialer/oem/MotorolaHiddenMenuKeySequence.java @@ -0,0 +1,153 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * This file is derived in part from code issued under the following license. + * + * 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.oem; + +import android.content.ActivityNotFoundException; +import android.content.Context; +import android.content.Intent; +import android.content.pm.ResolveInfo; +import com.android.dialer.common.LogUtil; +import java.util.regex.Pattern; + +/** + * Util class to handle special char sequence and launch corresponding intent based the sequence. + */ +public class MotorolaHiddenMenuKeySequence { + private static final String EXTRA_HIDDEN_MENU_CODE = "HiddenMenuCode"; + private static MotorolaHiddenMenuKeySequence instance = null; + + private static String[] hiddenKeySequenceArray = null; + private static String[] hiddenKeySequenceIntentArray = null; + private static String[] hiddenKeyPatternArray = null; + private static String[] hiddenKeyPatternIntentArray = null; + private static boolean featureHiddenMenuEnabled = false; + + /** + * Handle input char sequence. + * + * @param context context + * @param input input sequence + * @return true if the input matches any pattern + */ + static boolean handleCharSequence(Context context, String input) { + getInstance(context); + if (!featureHiddenMenuEnabled) { + return false; + } + return handleKeySequence(context, input) || handleKeyPattern(context, input); + } + + /** + * Public interface to return the Singleton instance + * + * @param context the Context + * @return the MotorolaHiddenMenuKeySequence singleton instance + */ + private static synchronized MotorolaHiddenMenuKeySequence getInstance(Context context) { + if (null == instance) { + instance = new MotorolaHiddenMenuKeySequence(context); + } + return instance; + } + + private MotorolaHiddenMenuKeySequence(Context context) { + featureHiddenMenuEnabled = + context.getResources().getBoolean(R.bool.motorola_feature_hidden_menu); + // In case we do have a SPN from resource we need to match from service; otherwise we are + // free to go + if (featureHiddenMenuEnabled) { + + hiddenKeySequenceArray = + context.getResources().getStringArray(R.array.motorola_hidden_menu_key_sequence); + hiddenKeySequenceIntentArray = + context.getResources().getStringArray(R.array.motorola_hidden_menu_key_sequence_intents); + hiddenKeyPatternArray = + context.getResources().getStringArray(R.array.motorola_hidden_menu_key_pattern); + hiddenKeyPatternIntentArray = + context.getResources().getStringArray(R.array.motorola_hidden_menu_key_pattern_intents); + + if (hiddenKeySequenceArray.length != hiddenKeySequenceIntentArray.length + || hiddenKeyPatternArray.length != hiddenKeyPatternIntentArray.length + || (hiddenKeySequenceArray.length == 0 && hiddenKeyPatternArray.length == 0)) { + LogUtil.e( + "MotorolaHiddenMenuKeySequence", + "the key sequence array is not matching, turn off feature." + + "key sequence: %d != %d, key pattern %d != %d", + hiddenKeySequenceArray.length, + hiddenKeySequenceIntentArray.length, + hiddenKeyPatternArray.length, + hiddenKeyPatternIntentArray.length); + featureHiddenMenuEnabled = false; + } + } + } + + private static boolean handleKeyPattern(Context context, String input) { + int len = input.length(); + if (len <= 3 || hiddenKeyPatternArray == null || hiddenKeyPatternIntentArray == null) { + return false; + } + + for (int i = 0; i < hiddenKeyPatternArray.length; i++) { + if ((Pattern.compile(hiddenKeyPatternArray[i])).matcher(input).matches()) { + return sendIntent(context, input, hiddenKeyPatternIntentArray[i]); + } + } + return false; + } + + private static boolean handleKeySequence(Context context, String input) { + int len = input.length(); + if (len <= 3 || hiddenKeySequenceArray == null || hiddenKeySequenceIntentArray == null) { + return false; + } + + for (int i = 0; i < hiddenKeySequenceArray.length; i++) { + if (hiddenKeySequenceArray[i].equals(input)) { + return sendIntent(context, input, hiddenKeySequenceIntentArray[i]); + } + } + return false; + } + + private static boolean sendIntent( + final Context context, final String input, final String action) { + LogUtil.d("MotorolaHiddenMenuKeySequence.sendIntent", "input: %s", input); + try { + Intent intent = new Intent(action); + intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); + intent.putExtra(EXTRA_HIDDEN_MENU_CODE, input); + + ResolveInfo resolveInfo = context.getPackageManager().resolveActivity(intent, 0); + + if (resolveInfo != null + && resolveInfo.activityInfo != null + && resolveInfo.activityInfo.enabled) { + context.startActivity(intent); + return true; + } else { + LogUtil.w("MotorolaHiddenMenuKeySequence.sendIntent", "not able to resolve the intent"); + } + } catch (ActivityNotFoundException e) { + LogUtil.e( + "MotorolaHiddenMenuKeySequence.sendIntent", "handleHiddenMenu Key Pattern Exception", e); + } + return false; + } +} diff --git a/java/com/android/dialer/oem/MotorolaUtils.java b/java/com/android/dialer/oem/MotorolaUtils.java new file mode 100644 index 000000000..29bf0b23d --- /dev/null +++ b/java/com/android/dialer/oem/MotorolaUtils.java @@ -0,0 +1,51 @@ +package com.android.dialer.oem; + +import android.content.Context; +import com.android.dialer.common.ConfigProviderBindings; + +/** Util class for Motorola OEM devices. */ +public class MotorolaUtils { + + private static final String CONFIG_HD_CODEC_BLINKING_ICON_WHEN_CONNECTING_CALL_ENABLED = + "hd_codec_blinking_icon_when_connecting_enabled"; + private static final String CONFIG_HD_CODEC_SHOW_ICON_IN_CALL_LOG_ENABLED = + "hd_codec_show_icon_in_call_log_enabled"; + + // This is used to check if a Motorola device supports HD voice call feature, which comes from + // system feature setting. + private static final String HD_CALL_FEATRURE = "com.motorola.software.sprint.hd_call"; + + // Feature flag indicates it's a HD call, currently this is only used by Motorola system build. + // TODO(b/35359461): Upstream and move it to android.provider.CallLog. + private static final int FEATURES_HD_CALL = 0x10000000; + + public static boolean shouldBlinkHdIconWhenConnectingCall(Context context) { + return ConfigProviderBindings.get(context) + .getBoolean(CONFIG_HD_CODEC_BLINKING_ICON_WHEN_CONNECTING_CALL_ENABLED, true) + && isSupportingSprintHdCodec(context); + } + + public static boolean shouldShowHdIconInCallLog(Context context, int features) { + return ConfigProviderBindings.get(context) + .getBoolean(CONFIG_HD_CODEC_SHOW_ICON_IN_CALL_LOG_ENABLED, true) + && isSupportingSprintHdCodec(context) + && (features & FEATURES_HD_CALL) == FEATURES_HD_CALL; + } + + /** + * Handle special char sequence entered in dialpad. This may launch special intent based on input. + * + * @param context context + * @param input input string + * @return true if the input is consumed and the intent is launched + */ + public static boolean handleSpecialCharSequence(Context context, String input) { + // TODO(b/35395377): Add check for Motorola devices. + return MotorolaHiddenMenuKeySequence.handleCharSequence(context, input); + } + + private static boolean isSupportingSprintHdCodec(Context context) { + return context.getPackageManager().hasSystemFeature(HD_CALL_FEATRURE) + && context.getResources().getBoolean(R.bool.motorola_sprint_hd_codec); + } +} diff --git a/java/com/android/dialer/oem/res/values/motorola_config.xml b/java/com/android/dialer/oem/res/values/motorola_config.xml new file mode 100644 index 000000000..f875d573d --- /dev/null +++ b/java/com/android/dialer/oem/res/values/motorola_config.xml @@ -0,0 +1,64 @@ + + + + false + + + + false + + + + ##66236# + ##2539# + ##786# + ##72786# + ##3282# + ##33284# + ##3424# + ##564# + ##4567257# + ##873283# + ##6343# + ##27263# + ##258# + ##8422# + ##4382# + + + com.motorola.intent.action.LAUNCH_HIDDEN_MENU + + + + @string/motorola_hidden_menu_intent + @string/motorola_hidden_menu_intent + @string/motorola_hidden_menu_intent + @string/motorola_hidden_menu_intent + @string/motorola_hidden_menu_intent + @string/motorola_hidden_menu_intent + @string/motorola_hidden_menu_intent + @string/motorola_hidden_menu_intent + @string/motorola_hidden_menu_intent + com.motorola.android.intent.action.omadm.sprint.hfa + @string/motorola_hidden_menu_intent + @string/motorola_hidden_menu_intent + @string/motorola_hidden_menu_intent + @string/motorola_hidden_menu_intent + @string/motorola_hidden_menu_intent + + + + + + ##[0-9]{6}# + + + + + @string/motorola_hidden_menu_intent + + \ No newline at end of file diff --git a/java/com/android/dialer/p13n/inference/P13nRanking.java b/java/com/android/dialer/p13n/inference/P13nRanking.java index 6bfc0352a..0682e85db 100644 --- a/java/com/android/dialer/p13n/inference/P13nRanking.java +++ b/java/com/android/dialer/p13n/inference/P13nRanking.java @@ -22,6 +22,7 @@ import android.support.annotation.MainThread; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import com.android.dialer.common.Assert; +import com.android.dialer.common.ConfigProviderBindings; import com.android.dialer.p13n.inference.protocol.P13nRanker; import com.android.dialer.p13n.inference.protocol.P13nRankerFactory; import java.util.List; @@ -38,37 +39,51 @@ public final class P13nRanking { public static P13nRanker get(@NonNull Context context) { Assert.isNotNull(context); Assert.isMainThread(); + if (ranker != null) { return ranker; } + if (!ConfigProviderBindings.get(context).getBoolean("p13n_ranker_should_enable", false)) { + setToIdentityRanker(); + return ranker; + } + Context application = context.getApplicationContext(); if (application instanceof P13nRankerFactory) { ranker = ((P13nRankerFactory) application).newP13nRanker(); } if (ranker == null) { - ranker = - new P13nRanker() { - @Override - public void refresh(@Nullable P13nRefreshCompleteListener listener) {} - - @Override - public List rankList(List phoneNumbers) { - return phoneNumbers; - } - - @NonNull - @Override - public Cursor rankCursor( - @NonNull Cursor phoneQueryResults, int phoneNumberColumnIndex) { - return phoneQueryResults; - } - }; + setToIdentityRanker(); } return ranker; } + private static void setToIdentityRanker() { + ranker = + new P13nRanker() { + @Override + public void refresh(@Nullable P13nRefreshCompleteListener listener) {} + + @Override + public List rankList(List phoneNumbers) { + return phoneNumbers; + } + + @NonNull + @Override + public Cursor rankCursor(@NonNull Cursor phoneQueryResults, int queryLength) { + return phoneQueryResults; + } + + @Override + public boolean shouldShowEmptyListForNullQuery() { + return true; + } + }; + } + public static void setForTesting(@NonNull P13nRanker ranker) { P13nRanking.ranker = ranker; } diff --git a/java/com/android/dialer/p13n/inference/protocol/P13nRanker.java b/java/com/android/dialer/p13n/inference/protocol/P13nRanker.java index 9a859a6db..41f1de49d 100644 --- a/java/com/android/dialer/p13n/inference/protocol/P13nRanker.java +++ b/java/com/android/dialer/p13n/inference/protocol/P13nRanker.java @@ -41,13 +41,15 @@ public interface P13nRanker { * input cursor is closed or invalid, or if any other error occurs in the ranking process. * * @param phoneQueryResults cursor of results of a Dialer search query - * @param phoneNumberColumnIndex column index of the phone number in the cursor data + * @param queryLength length of the search query that resulted in the cursor data, if below 0, + * assumes no length is specified, thus applies the default behavior which is same as when + * queryLength is greater than zero. * @return new cursor of data reordered by ranking (or reference to input cursor if order * unchanged) */ @NonNull @MainThread - Cursor rankCursor(@NonNull Cursor phoneQueryResults, int phoneNumberColumnIndex); + Cursor rankCursor(@NonNull Cursor phoneQueryResults, int queryLength); /** * Refreshes ranking cache (pulls fresh contextual features, pre-caches inference results, etc.). @@ -61,6 +63,10 @@ public interface P13nRanker { @MainThread void refresh(@Nullable P13nRefreshCompleteListener listener); + /** Decides if results should be displayed for no-query search. */ + @MainThread + boolean shouldShowEmptyListForNullQuery(); + /** * Callback class for when ranking refresh has completed. * diff --git a/java/com/android/dialer/phonenumbercache/CachedNumberLookupService.java b/java/com/android/dialer/phonenumbercache/CachedNumberLookupService.java index 03b77b91c..f443d56fb 100644 --- a/java/com/android/dialer/phonenumbercache/CachedNumberLookupService.java +++ b/java/com/android/dialer/phonenumbercache/CachedNumberLookupService.java @@ -18,7 +18,9 @@ package com.android.dialer.phonenumbercache; import android.content.Context; import android.net.Uri; +import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.support.annotation.WorkerThread; import java.io.InputStream; public interface CachedNumberLookupService { @@ -35,6 +37,7 @@ public interface CachedNumberLookupService { * found in the cache, {@link ContactInfo#EMPTY} if the phone number was not found in the * cache, and null if there was an error when querying the cache. */ + @WorkerThread CachedContactInfo lookupCachedContactFromNumber(Context context, String number); void addContact(Context context, CachedContactInfo info); @@ -64,6 +67,7 @@ public interface CachedNumberLookupService { int SOURCE_TYPE_PROFILE = 4; int SOURCE_TYPE_CNAP = 5; + @NonNull ContactInfo getContactInfo(); void setSource(int sourceType, String name, long directoryId); diff --git a/java/com/android/dialer/postcall/AndroidManifest.xml b/java/com/android/dialer/postcall/AndroidManifest.xml new file mode 100644 index 000000000..2bf07bca2 --- /dev/null +++ b/java/com/android/dialer/postcall/AndroidManifest.xml @@ -0,0 +1,28 @@ + + + + + + + diff --git a/java/com/android/dialer/postcall/PostCall.java b/java/com/android/dialer/postcall/PostCall.java new file mode 100644 index 000000000..cfe7c867b --- /dev/null +++ b/java/com/android/dialer/postcall/PostCall.java @@ -0,0 +1,182 @@ +/* + * 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.postcall; + +import android.app.Activity; +import android.content.Context; +import android.content.Intent; +import android.content.SharedPreferences; +import android.preference.PreferenceManager; +import android.support.design.widget.BaseTransientBottomBar.BaseCallback; +import android.support.design.widget.Snackbar; +import android.view.View; +import android.view.View.OnClickListener; +import com.android.dialer.buildtype.BuildType; +import com.android.dialer.common.Assert; +import com.android.dialer.common.ConfigProvider; +import com.android.dialer.common.ConfigProviderBindings; +import com.android.dialer.common.LogUtil; +import com.android.dialer.logging.Logger; +import com.android.dialer.logging.nano.DialerImpression; +import com.android.dialer.util.DialerUtils; +import com.android.dialer.util.IntentUtil; + +/** Helper class to handle all post call actions. */ +public class PostCall { + + private static final String KEY_POST_CALL_CALL_CONNECT_TIME = "post_call_call_connect_time"; + private static final String KEY_POST_CALL_CALL_DISCONNECT_TIME = "post_call_call_disconnect_time"; + private static final String KEY_POST_CALL_CALL_NUMBER = "post_call_call_number"; + private static final String KEY_POST_CALL_MESSAGE_SENT = "post_call_message_sent"; + + public static void promptUserForMessageIfNecessary(Activity activity, View rootView) { + if (isEnabled(activity)) { + if (shouldPromptUserToViewSentMessage(activity)) { + promptUserToViewSentMessage(activity, rootView); + } else if (shouldPromptUserToSendMessage(activity)) { + promptUserToSendMessage(activity, rootView); + } + } + } + + private static void promptUserToSendMessage(Activity activity, View rootView) { + LogUtil.i("PostCall.promptUserToSendMessage", "returned from call, showing post call SnackBar"); + String message = activity.getString(R.string.post_call_message); + String addMessage = activity.getString(R.string.post_call_add_message); + OnClickListener onClickListener = + v -> { + Logger.get(activity) + .logImpression(DialerImpression.Type.POST_CALL_PROMPT_USER_TO_SEND_MESSAGE_CLICKED); + activity.startActivity(PostCallActivity.newIntent(activity, getPhoneNumber(activity))); + }; + + Snackbar.make(rootView, message, Snackbar.LENGTH_INDEFINITE) + .setAction(addMessage, onClickListener) + .setActionTextColor( + activity.getResources().getColor(R.color.dialer_snackbar_action_text_color)) + .show(); + Logger.get(activity).logImpression(DialerImpression.Type.POST_CALL_PROMPT_USER_TO_SEND_MESSAGE); + PreferenceManager.getDefaultSharedPreferences(activity) + .edit() + .remove(KEY_POST_CALL_CALL_DISCONNECT_TIME) + .apply(); + } + + private static void promptUserToViewSentMessage(Activity activity, View rootView) { + LogUtil.i( + "PostCall.promptUserToViewSentMessage", + "returned from sending a post call message, message sent."); + String message = activity.getString(R.string.post_call_message_sent); + String addMessage = activity.getString(R.string.view); + OnClickListener onClickListener = + v -> { + Logger.get(activity) + .logImpression( + DialerImpression.Type.POST_CALL_PROMPT_USER_TO_VIEW_SENT_MESSAGE_CLICKED); + Intent intent = IntentUtil.getSendSmsIntent(getPhoneNumber(activity)); + DialerUtils.startActivityWithErrorToast(activity, intent); + }; + + Snackbar.make(rootView, message, Snackbar.LENGTH_INDEFINITE) + .setAction(addMessage, onClickListener) + .setActionTextColor( + activity.getResources().getColor(R.color.dialer_snackbar_action_text_color)) + .addCallback( + new BaseCallback() { + @Override + public void onDismissed(Snackbar snackbar, int i) { + super.onDismissed(snackbar, i); + clear(snackbar.getContext()); + } + }) + .show(); + Logger.get(activity) + .logImpression(DialerImpression.Type.POST_CALL_PROMPT_USER_TO_VIEW_SENT_MESSAGE); + PreferenceManager.getDefaultSharedPreferences(activity) + .edit() + .remove(KEY_POST_CALL_MESSAGE_SENT) + .apply(); + } + + public static void onCallDisconnected(Context context, String number, long callConnectedMillis) { + PreferenceManager.getDefaultSharedPreferences(context) + .edit() + .putLong(KEY_POST_CALL_CALL_CONNECT_TIME, callConnectedMillis) + .putLong(KEY_POST_CALL_CALL_DISCONNECT_TIME, System.currentTimeMillis()) + .putString(KEY_POST_CALL_CALL_NUMBER, number) + .apply(); + } + + public static void onMessageSent(Context context, String number) { + PreferenceManager.getDefaultSharedPreferences(context) + .edit() + .putString(KEY_POST_CALL_CALL_NUMBER, number) + .putBoolean(KEY_POST_CALL_MESSAGE_SENT, true) + .apply(); + } + + private static void clear(Context context) { + PreferenceManager.getDefaultSharedPreferences(context) + .edit() + .remove(KEY_POST_CALL_CALL_DISCONNECT_TIME) + .remove(KEY_POST_CALL_CALL_NUMBER) + .remove(KEY_POST_CALL_MESSAGE_SENT) + .remove(KEY_POST_CALL_CALL_CONNECT_TIME) + .apply(); + } + + private static boolean shouldPromptUserToSendMessage(Context context) { + SharedPreferences manager = PreferenceManager.getDefaultSharedPreferences(context); + long disconnectTimeMillis = manager.getLong(KEY_POST_CALL_CALL_DISCONNECT_TIME, -1); + long connectTimeMillis = manager.getLong(KEY_POST_CALL_CALL_CONNECT_TIME, -1); + + long timeSinceDisconnect = System.currentTimeMillis() - disconnectTimeMillis; + long callDurationMillis = disconnectTimeMillis - connectTimeMillis; + + ConfigProvider binding = ConfigProviderBindings.get(context); + return disconnectTimeMillis != -1 + && connectTimeMillis != -1 + && binding.getLong("postcall_last_call_threshold", 30_000) > timeSinceDisconnect + && binding.getLong("postcall_call_duration_threshold", 60_000) > callDurationMillis; + } + + private static boolean shouldPromptUserToViewSentMessage(Context context) { + return PreferenceManager.getDefaultSharedPreferences(context) + .getBoolean(KEY_POST_CALL_MESSAGE_SENT, false); + } + + private static String getPhoneNumber(Context context) { + return PreferenceManager.getDefaultSharedPreferences(context) + .getString(KEY_POST_CALL_CALL_NUMBER, null); + } + + private static boolean isEnabled(Context context) { + @BuildType.Type int type = BuildType.get(); + switch (type) { + case BuildType.BUGFOOD: + case BuildType.DOGFOOD: + case BuildType.FISHFOOD: + case BuildType.TEST: + return ConfigProviderBindings.get(context).getBoolean("enable_post_call", true); + case BuildType.RELEASE: + return ConfigProviderBindings.get(context).getBoolean("enable_post_call_prod", true); + default: + Assert.fail(); + return false; + } + } +} diff --git a/java/com/android/dialer/postcall/PostCallActivity.java b/java/com/android/dialer/postcall/PostCallActivity.java new file mode 100644 index 000000000..8da03dcd1 --- /dev/null +++ b/java/com/android/dialer/postcall/PostCallActivity.java @@ -0,0 +1,151 @@ +/* + * 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.postcall; + +import android.Manifest.permission; +import android.content.Context; +import android.content.Intent; +import android.content.pm.PackageManager; +import android.net.Uri; +import android.os.Bundle; +import android.provider.Settings; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.support.v7.app.AppCompatActivity; +import android.telephony.SmsManager; +import android.widget.Toolbar; +import com.android.dialer.common.Assert; +import com.android.dialer.common.LogUtil; +import com.android.dialer.enrichedcall.EnrichedCallCapabilities; +import com.android.dialer.enrichedcall.EnrichedCallComponent; +import com.android.dialer.enrichedcall.EnrichedCallManager; +import com.android.dialer.util.PermissionsUtil; +import com.android.dialer.widget.MessageFragment; + +/** Activity used to send post call messages after a phone call. */ +public class PostCallActivity extends AppCompatActivity implements MessageFragment.Listener { + + public static final String KEY_PHONE_NUMBER = "phone_number"; + public static final String KEY_MESSAGE = "message"; + private static final int REQUEST_CODE_SEND_SMS = 1; + + private boolean useRcs; + + public static Intent newIntent(@NonNull Context context, @NonNull String number) { + Intent intent = new Intent(Assert.isNotNull(context), PostCallActivity.class); + intent.putExtra(KEY_PHONE_NUMBER, Assert.isNotNull(number)); + return intent; + } + + @Override + protected void onCreate(@Nullable Bundle bundle) { + super.onCreate(bundle); + setContentView(R.layout.post_call_activity); + + Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); + toolbar.setTitle(getString(R.string.post_call_message)); + toolbar.setNavigationOnClickListener(v -> finish()); + + useRcs = canUseRcs(getIntent().getStringExtra(KEY_PHONE_NUMBER)); + LogUtil.i("PostCallActivity.onCreate", "useRcs: %b", useRcs); + + int postCallCharLimit = + useRcs + ? getResources().getInteger(R.integer.post_call_char_limit) + : MessageFragment.NO_CHAR_LIMIT; + String[] messages = + new String[] { + getString(R.string.post_call_message_1), + getString(R.string.post_call_message_2), + getString(R.string.post_call_message_3) + }; + MessageFragment fragment = + MessageFragment.builder() + .setCharLimit(postCallCharLimit) + .showSendIcon() + .setMessages(messages) + .build(); + getSupportFragmentManager() + .beginTransaction() + .replace(R.id.message_container, fragment) + .commit(); + } + + private boolean canUseRcs(@NonNull String number) { + EnrichedCallCapabilities capabilities = + getEnrichedCallManager().getCapabilities(Assert.isNotNull(number)); + LogUtil.i( + "PostCallActivity.canUseRcs", + "number: %s, capabilities: %s", + LogUtil.sanitizePhoneNumber(number), + capabilities); + return capabilities != null && capabilities.supportsPostCall(); + } + + @Override + public void onMessageFragmentSendMessage(@NonNull String message) { + String number = Assert.isNotNull(getIntent().getStringExtra(KEY_PHONE_NUMBER)); + getIntent().putExtra(KEY_MESSAGE, message); + + if (useRcs) { + LogUtil.i("PostCallActivity.onMessageFragmentSendMessage", "sending post call Rcs."); + getEnrichedCallManager().sendPostCallNote(number, message); + PostCall.onMessageSent(this, number); + finish(); + } else if (PermissionsUtil.hasPermission(this, permission.SEND_SMS)) { + LogUtil.i("PostCallActivity.sendMessage", "Sending post call SMS."); + SmsManager smsManager = SmsManager.getDefault(); + smsManager.sendMultipartTextMessage( + number, null, smsManager.divideMessage(message), null, null); + PostCall.onMessageSent(this, number); + finish(); + } else if (PermissionsUtil.isFirstRequest(this, permission.SEND_SMS) + || shouldShowRequestPermissionRationale(permission.SEND_SMS)) { + LogUtil.i("PostCallActivity.sendMessage", "Requesting SMS_SEND permission."); + requestPermissions(new String[] {permission.SEND_SMS}, REQUEST_CODE_SEND_SMS); + } else { + LogUtil.i( + "PostCallActivity.sendMessage", "Permission permanently denied, sending to settings."); + Intent intent = new Intent(Intent.ACTION_VIEW); + intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + intent.setData(Uri.parse("package:" + this.getPackageName())); + startActivity(intent); + } + } + + @Override + public void onMessageFragmentAfterTextChange(String message) {} + + @Override + public void onRequestPermissionsResult( + int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { + if (permissions.length > 0 && permissions[0].equals(permission.SEND_SMS)) { + PermissionsUtil.permissionRequested(this, permissions[0]); + } + if (requestCode == REQUEST_CODE_SEND_SMS + && grantResults.length > 0 + && grantResults[0] == PackageManager.PERMISSION_GRANTED) { + onMessageFragmentSendMessage(getIntent().getStringExtra(KEY_MESSAGE)); + } + } + + @NonNull + private EnrichedCallManager getEnrichedCallManager() { + return EnrichedCallComponent.get(this).getEnrichedCallManager(); + } +} diff --git a/java/com/android/dialer/postcall/res/layout/post_call_activity.xml b/java/com/android/dialer/postcall/res/layout/post_call_activity.xml new file mode 100644 index 000000000..6ea8126c5 --- /dev/null +++ b/java/com/android/dialer/postcall/res/layout/post_call_activity.xml @@ -0,0 +1,38 @@ + + + + + + + + \ No newline at end of file diff --git a/java/com/android/dialer/postcall/res/values/strings.xml b/java/com/android/dialer/postcall/res/values/strings.xml new file mode 100644 index 000000000..d5e085a05 --- /dev/null +++ b/java/com/android/dialer/postcall/res/values/strings.xml @@ -0,0 +1,31 @@ + + + + + Say why you called + + This is urgent. Call me back. + + Call me back when you have some time. + + Not urgent, we can chat later. + + Add message + + Message sent + View + \ No newline at end of file diff --git a/java/com/android/dialer/postcall/res/values/values.xml b/java/com/android/dialer/postcall/res/values/values.xml new file mode 100644 index 000000000..64fe9f6c8 --- /dev/null +++ b/java/com/android/dialer/postcall/res/values/values.xml @@ -0,0 +1,19 @@ + + + + 60 + \ No newline at end of file diff --git a/java/com/android/dialer/proguard/proguard.flags b/java/com/android/dialer/proguard/proguard.flags new file mode 100644 index 000000000..0f684a0b3 --- /dev/null +++ b/java/com/android/dialer/proguard/proguard.flags @@ -0,0 +1,6 @@ +# Keep the annotation, classes, methods, and fields marked as UsedByReflection +-keep class com.android.dialer.proguard.UsedByReflection +-keep @com.android.dialer.proguard.UsedByReflection class * +-keepclassmembers class * { + @com.android.dialer.proguard.UsedByReflection *; +} diff --git a/java/com/android/dialer/proguard/proguard_base.flags b/java/com/android/dialer/proguard/proguard_base.flags new file mode 100644 index 000000000..7b5794ec7 --- /dev/null +++ b/java/com/android/dialer/proguard/proguard_base.flags @@ -0,0 +1,74 @@ +# Copied from http://google3/java/com/google/android/apps/common/proguard/base.flags + +# This file is intended to contain proguard options that *nobody* would ever +# not want, in *any* configuration - they ensure basic correctness, and have +# no downsides. You probably do not want to make changes to this file. + +# The presence of both of these attributes causes dalvik and other jvms to print +# stack traces on uncaught exceptions, which is necessary to get useful crash +# reports. +-keepattributes SourceFile,LineNumberTable + +# Preverification was introduced in Java 6 to enable faster classloading, but +# dex doesn't use the java .class format, so it has no benefit and can cause +# problems. +-dontpreverify + +# Skipping analysis of some classes may make proguard strip something that's +# needed. +-dontskipnonpubliclibraryclasses + +# Case-insensitive filesystems can't handle when a.class and A.class exist in +# the same directory. +-dontusemixedcaseclassnames + +# This prevents the names of native methods from being obfuscated and prevents +# UnsatisfiedLinkErrors. +-keepclasseswithmembernames class * { + native ; +} + +# hackbod discourages the use of enums on android, but if you use them, they +# should work. Allow instantiation via reflection by keeping the values method. +-keepclassmembers enum * { + public static **[] values(); +} + +# Parcel reflectively accesses this field. +-keepclassmembers class * implements android.os.Parcelable { + public static *** CREATOR; +} + +# These methods are needed to ensure that serialization behaves as expected when +# classes are obfuscated, shrunk, and/or optimized. +-keepclassmembers class * implements java.io.Serializable { + static final long serialVersionUID; + private static final java.io.ObjectStreamField[] serialPersistentFields; + private void writeObject(java.io.ObjectOutputStream); + private void readObject(java.io.ObjectInputStream); + java.lang.Object writeReplace(); + java.lang.Object readResolve(); +} + +# Don't warn about Guava. Any Guava-using app will fail the proguard stage without this dontwarn, +# and since Guava is so widely used, we include it here in the base. +-dontwarn com.google.common.** + +# Don't warn about Error Prone annotations (e.g. @CompileTimeConstant) +-dontwarn com.google.errorprone.annotations.** + +# Based on http://ag/718466: android.app.Notification.setLatestEventInfo() was +# removed in MNC, but is still referenced (safely) by the NotificationCompat +# code. +-dontwarn android.app.Notification + +# Silence notes about dynamically referenced classes from AOSP support +# libraries. +-dontnote android.graphics.Insets + +# AOSP support library: ICU references to gender and plurals messages. +-dontnote libcore.icu.ICU +-keep class libcore.icu.ICU { *** get(...);} + +# AOSP support library: Handle classes that use reflection. +-dontnote android.support.v4.app.NotificationCompatJellybean diff --git a/java/com/android/dialer/proguard/proguard_release.flags b/java/com/android/dialer/proguard/proguard_release.flags new file mode 100644 index 000000000..1c845cfa3 --- /dev/null +++ b/java/com/android/dialer/proguard/proguard_release.flags @@ -0,0 +1,24 @@ +# Copied from http://google3/java/com/google/android/apps/common/proguard/release.flags + +# Used for building release binaries. Obfuscates, optimizes, and shrinks. + +# By default, proguard leaves all classes in their original package, which +# needlessly repeats com.google.android.apps.etc. +-repackageclasses "" + +# Allows proguard to make private and protected methods and fields public as +# part of optimization. This lets proguard inline trivial getter/setter methods. +-allowaccessmodification + +# The source file attribute must be present in order to print stack traces, but +# we rename it in order to avoid leaking the pre-obfuscation class name. +-renamesourcefileattribute PG + +# This allows proguard to strip isLoggable() blocks containing only debug log +# code from release builds. +-assumenosideeffects class android.util.Log { + static *** i(...); + static *** d(...); + static *** v(...); + static *** isLoggable(...); +} diff --git a/java/com/android/dialer/shortcuts/AutoValue_DialerShortcut.java b/java/com/android/dialer/shortcuts/AutoValue_DialerShortcut.java deleted file mode 100644 index ef995c816..000000000 --- a/java/com/android/dialer/shortcuts/AutoValue_DialerShortcut.java +++ /dev/null @@ -1,161 +0,0 @@ -/* - * 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.shortcuts; - -import android.support.annotation.NonNull; -import javax.annotation.Generated; - -@Generated("com.google.auto.value.processor.AutoValueProcessor") - final class AutoValue_DialerShortcut extends DialerShortcut { - - private final long contactId; - private final String lookupKey; - private final String displayName; - private final int rank; - - private AutoValue_DialerShortcut( - long contactId, - String lookupKey, - String displayName, - int rank) { - this.contactId = contactId; - this.lookupKey = lookupKey; - this.displayName = displayName; - this.rank = rank; - } - - @Override - long getContactId() { - return contactId; - } - - @NonNull - @Override - String getLookupKey() { - return lookupKey; - } - - @NonNull - @Override - String getDisplayName() { - return displayName; - } - - @Override - int getRank() { - return rank; - } - - @Override - public String toString() { - return "DialerShortcut{" - + "contactId=" + contactId + ", " - + "lookupKey=" + lookupKey + ", " - + "displayName=" + displayName + ", " - + "rank=" + rank - + "}"; - } - - @Override - public boolean equals(Object o) { - if (o == this) { - return true; - } - if (o instanceof DialerShortcut) { - DialerShortcut that = (DialerShortcut) o; - return (this.contactId == that.getContactId()) - && (this.lookupKey.equals(that.getLookupKey())) - && (this.displayName.equals(that.getDisplayName())) - && (this.rank == that.getRank()); - } - return false; - } - - @Override - public int hashCode() { - int h = 1; - h *= 1000003; - h ^= (this.contactId >>> 32) ^ this.contactId; - h *= 1000003; - h ^= this.lookupKey.hashCode(); - h *= 1000003; - h ^= this.displayName.hashCode(); - h *= 1000003; - h ^= this.rank; - return h; - } - - static final class Builder extends DialerShortcut.Builder { - private Long contactId; - private String lookupKey; - private String displayName; - private Integer rank; - Builder() { - } - private Builder(DialerShortcut source) { - this.contactId = source.getContactId(); - this.lookupKey = source.getLookupKey(); - this.displayName = source.getDisplayName(); - this.rank = source.getRank(); - } - @Override - DialerShortcut.Builder setContactId(long contactId) { - this.contactId = contactId; - return this; - } - @Override - DialerShortcut.Builder setLookupKey(String lookupKey) { - this.lookupKey = lookupKey; - return this; - } - @Override - DialerShortcut.Builder setDisplayName(String displayName) { - this.displayName = displayName; - return this; - } - @Override - DialerShortcut.Builder setRank(int rank) { - this.rank = rank; - return this; - } - @Override - DialerShortcut build() { - String missing = ""; - if (this.contactId == null) { - missing += " contactId"; - } - if (this.lookupKey == null) { - missing += " lookupKey"; - } - if (this.displayName == null) { - missing += " displayName"; - } - if (this.rank == null) { - missing += " rank"; - } - if (!missing.isEmpty()) { - throw new IllegalStateException("Missing required properties:" + missing); - } - return new AutoValue_DialerShortcut( - this.contactId, - this.lookupKey, - this.displayName, - this.rank); - } - } - -} \ No newline at end of file diff --git a/java/com/android/dialer/shortcuts/CallContactActivity.java b/java/com/android/dialer/shortcuts/CallContactActivity.java index 1e9a01b39..40bf97b87 100644 --- a/java/com/android/dialer/shortcuts/CallContactActivity.java +++ b/java/com/android/dialer/shortcuts/CallContactActivity.java @@ -56,11 +56,20 @@ public class CallContactActivity extends TransactionSafeActivity } } + /** + * Attempt to make a call, finishing the activity if the required permissions are already granted. + * If the required permissions are not already granted, the activity is not finished so that the + * user can choose to grant or deny them. + */ private void makeCall() { CallSpecificAppData callSpecificAppData = new CallSpecificAppData(); callSpecificAppData.callInitiationType = CallInitiationType.Type.LAUNCHER_SHORTCUT; - PhoneNumberInteraction.startInteractionForPhoneCall( - this, contactUri, false /* isVideoCall */, callSpecificAppData); + boolean interactionStarted = + PhoneNumberInteraction.startInteractionForPhoneCall( + this, contactUri, false /* isVideoCall */, callSpecificAppData); + if (interactionStarted) { + finish(); + } } @Override @@ -115,6 +124,7 @@ public class CallContactActivity extends TransactionSafeActivity int requestCode, String[] permissions, int[] grantResults) { switch (requestCode) { case PhoneNumberInteraction.REQUEST_READ_CONTACTS: + case PhoneNumberInteraction.REQUEST_CALL_PHONE: { // If request is cancelled, the result arrays are empty. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { @@ -122,8 +132,8 @@ public class CallContactActivity extends TransactionSafeActivity } else { Toast.makeText(this, R.string.dialer_shortcut_no_permissions, Toast.LENGTH_SHORT) .show(); + finish(); } - finish(); break; } default: diff --git a/java/com/android/dialer/shortcuts/DialerShortcut.java b/java/com/android/dialer/shortcuts/DialerShortcut.java index f2fb3301a..a8d4204fe 100644 --- a/java/com/android/dialer/shortcuts/DialerShortcut.java +++ b/java/com/android/dialer/shortcuts/DialerShortcut.java @@ -22,7 +22,7 @@ import android.net.Uri; import android.os.Build.VERSION_CODES; import android.provider.ContactsContract.Contacts; import android.support.annotation.NonNull; - +import com.google.auto.value.AutoValue; /** * Convenience data structure. @@ -31,7 +31,7 @@ import android.support.annotation.NonNull; * convenience methods for doing things like constructing labels. */ @TargetApi(VERSION_CODES.N_MR1) // Shortcuts introduced in N MR1 - +@AutoValue abstract class DialerShortcut { /** Marker value indicates that shortcut has no setRank. Used by pinned shortcuts. */ @@ -160,7 +160,7 @@ abstract class DialerShortcut { return new AutoValue_DialerShortcut.Builder().setRank(NO_RANK); } - + @AutoValue.Builder abstract static class Builder { /** diff --git a/java/com/android/dialer/shortcuts/res/values/strings.xml b/java/com/android/dialer/shortcuts/res/values/strings.xml index 1e2c87f12..5f14a8100 100644 --- a/java/com/android/dialer/shortcuts/res/values/strings.xml +++ b/java/com/android/dialer/shortcuts/res/values/strings.xml @@ -30,8 +30,8 @@ be found or doesn't have any phone numbers. [CHAR LIMIT=70] --> Contact no longer available. - - Cannot call without contact permissions. + + Cannot call without permissions. diff --git a/java/com/android/dialer/shortcuts/res/xml/shortcuts.xml b/java/com/android/dialer/shortcuts/res/xml/shortcuts.xml index 5e8f58d1f..49149e3e1 100644 --- a/java/com/android/dialer/shortcuts/res/xml/shortcuts.xml +++ b/java/com/android/dialer/shortcuts/res/xml/shortcuts.xml @@ -24,8 +24,6 @@ + android:data="content://com.android.contacts/contacts"/> diff --git a/java/com/android/dialer/simulator/SimulatorComponent.java b/java/com/android/dialer/simulator/SimulatorComponent.java new file mode 100644 index 000000000..a16592e34 --- /dev/null +++ b/java/com/android/dialer/simulator/SimulatorComponent.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.simulator; + +import android.content.Context; +import dagger.Subcomponent; +import com.android.dialer.simulator.impl.SimulatorImpl; + +/** Subcomponent that can be used to access the simulator implementation. */ +public class SimulatorComponent { + private static SimulatorComponent instance; + private Simulator simulator; + + public Simulator getSimulator() { + if (simulator == null) { + simulator = new SimulatorImpl(); + } + return simulator; + } + + public static SimulatorComponent get(Context context) { + if (instance == null) { + instance = new SimulatorComponent(); + } + return instance; + } + + /** Used to refer to the root application component. */ + public interface HasComponent { + SimulatorComponent simulatorComponent(); + } +} diff --git a/java/com/android/dialer/simulator/impl/AutoValue_SimulatorCallLog_CallEntry.java b/java/com/android/dialer/simulator/impl/AutoValue_SimulatorCallLog_CallEntry.java deleted file mode 100644 index 591819819..000000000 --- a/java/com/android/dialer/simulator/impl/AutoValue_SimulatorCallLog_CallEntry.java +++ /dev/null @@ -1,160 +0,0 @@ -/* - * 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.simulator.impl; - -import android.support.annotation.NonNull; -import javax.annotation.Generated; - -@Generated("com.google.auto.value.processor.AutoValueProcessor") - final class AutoValue_SimulatorCallLog_CallEntry extends SimulatorCallLog.CallEntry { - - private final String number; - private final int type; - private final int presentation; - private final long timeMillis; - - private AutoValue_SimulatorCallLog_CallEntry( - String number, - int type, - int presentation, - long timeMillis) { - this.number = number; - this.type = type; - this.presentation = presentation; - this.timeMillis = timeMillis; - } - - @NonNull - @Override - String getNumber() { - return number; - } - - @Override - int getType() { - return type; - } - - @Override - int getPresentation() { - return presentation; - } - - @Override - long getTimeMillis() { - return timeMillis; - } - - @Override - public String toString() { - return "CallEntry{" - + "number=" + number + ", " - + "type=" + type + ", " - + "presentation=" + presentation + ", " - + "timeMillis=" + timeMillis - + "}"; - } - - @Override - public boolean equals(Object o) { - if (o == this) { - return true; - } - if (o instanceof SimulatorCallLog.CallEntry) { - SimulatorCallLog.CallEntry that = (SimulatorCallLog.CallEntry) o; - return (this.number.equals(that.getNumber())) - && (this.type == that.getType()) - && (this.presentation == that.getPresentation()) - && (this.timeMillis == that.getTimeMillis()); - } - return false; - } - - @Override - public int hashCode() { - int h = 1; - h *= 1000003; - h ^= this.number.hashCode(); - h *= 1000003; - h ^= this.type; - h *= 1000003; - h ^= this.presentation; - h *= 1000003; - h ^= (this.timeMillis >>> 32) ^ this.timeMillis; - return h; - } - - static final class Builder extends SimulatorCallLog.CallEntry.Builder { - private String number; - private Integer type; - private Integer presentation; - private Long timeMillis; - Builder() { - } - private Builder(SimulatorCallLog.CallEntry source) { - this.number = source.getNumber(); - this.type = source.getType(); - this.presentation = source.getPresentation(); - this.timeMillis = source.getTimeMillis(); - } - @Override - SimulatorCallLog.CallEntry.Builder setNumber(String number) { - this.number = number; - return this; - } - @Override - SimulatorCallLog.CallEntry.Builder setType(int type) { - this.type = type; - return this; - } - @Override - SimulatorCallLog.CallEntry.Builder setPresentation(int presentation) { - this.presentation = presentation; - return this; - } - @Override - SimulatorCallLog.CallEntry.Builder setTimeMillis(long timeMillis) { - this.timeMillis = timeMillis; - return this; - } - @Override - SimulatorCallLog.CallEntry build() { - String missing = ""; - if (this.number == null) { - missing += " number"; - } - if (this.type == null) { - missing += " type"; - } - if (this.presentation == null) { - missing += " presentation"; - } - if (this.timeMillis == null) { - missing += " timeMillis"; - } - if (!missing.isEmpty()) { - throw new IllegalStateException("Missing required properties:" + missing); - } - return new AutoValue_SimulatorCallLog_CallEntry( - this.number, - this.type, - this.presentation, - this.timeMillis); - } - } - -} diff --git a/java/com/android/dialer/simulator/impl/AutoValue_SimulatorContacts_Contact.java b/java/com/android/dialer/simulator/impl/AutoValue_SimulatorContacts_Contact.java deleted file mode 100644 index 00295f359..000000000 --- a/java/com/android/dialer/simulator/impl/AutoValue_SimulatorContacts_Contact.java +++ /dev/null @@ -1,231 +0,0 @@ -/* - * 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.simulator.impl; - -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import java.io.ByteArrayOutputStream; -import java.util.List; -import javax.annotation.Generated; - -@Generated("com.google.auto.value.processor.AutoValueProcessor") - final class AutoValue_SimulatorContacts_Contact extends SimulatorContacts.Contact { - - private final String accountType; - private final String accountName; - private final String name; - private final boolean isStarred; - private final ByteArrayOutputStream photoStream; - private final List phoneNumbers; - private final List emails; - - private AutoValue_SimulatorContacts_Contact( - String accountType, - String accountName, - @Nullable String name, - boolean isStarred, - @Nullable ByteArrayOutputStream photoStream, - List phoneNumbers, - List emails) { - this.accountType = accountType; - this.accountName = accountName; - this.name = name; - this.isStarred = isStarred; - this.photoStream = photoStream; - this.phoneNumbers = phoneNumbers; - this.emails = emails; - } - - @NonNull - @Override - String getAccountType() { - return accountType; - } - - @NonNull - @Override - String getAccountName() { - return accountName; - } - - @Nullable - @Override - String getName() { - return name; - } - - @Override - boolean getIsStarred() { - return isStarred; - } - - @Nullable - @Override - ByteArrayOutputStream getPhotoStream() { - return photoStream; - } - - @NonNull - @Override - List getPhoneNumbers() { - return phoneNumbers; - } - - @NonNull - @Override - List getEmails() { - return emails; - } - - @Override - public String toString() { - return "Contact{" - + "accountType=" + accountType + ", " - + "accountName=" + accountName + ", " - + "name=" + name + ", " - + "isStarred=" + isStarred + ", " - + "photoStream=" + photoStream + ", " - + "phoneNumbers=" + phoneNumbers + ", " - + "emails=" + emails - + "}"; - } - - @Override - public boolean equals(Object o) { - if (o == this) { - return true; - } - if (o instanceof SimulatorContacts.Contact) { - SimulatorContacts.Contact that = (SimulatorContacts.Contact) o; - return (this.accountType.equals(that.getAccountType())) - && (this.accountName.equals(that.getAccountName())) - && ((this.name == null) ? (that.getName() == null) : this.name.equals(that.getName())) - && (this.isStarred == that.getIsStarred()) - && ((this.photoStream == null) ? (that.getPhotoStream() == null) : this.photoStream.equals(that.getPhotoStream())) - && (this.phoneNumbers.equals(that.getPhoneNumbers())) - && (this.emails.equals(that.getEmails())); - } - return false; - } - - @Override - public int hashCode() { - int h = 1; - h *= 1000003; - h ^= this.accountType.hashCode(); - h *= 1000003; - h ^= this.accountName.hashCode(); - h *= 1000003; - h ^= (name == null) ? 0 : this.name.hashCode(); - h *= 1000003; - h ^= this.isStarred ? 1231 : 1237; - h *= 1000003; - h ^= (photoStream == null) ? 0 : this.photoStream.hashCode(); - h *= 1000003; - h ^= this.phoneNumbers.hashCode(); - h *= 1000003; - h ^= this.emails.hashCode(); - return h; - } - - static final class Builder extends SimulatorContacts.Contact.Builder { - private String accountType; - private String accountName; - private String name; - private Boolean isStarred; - private ByteArrayOutputStream photoStream; - private List phoneNumbers; - private List emails; - Builder() { - } - private Builder(SimulatorContacts.Contact source) { - this.accountType = source.getAccountType(); - this.accountName = source.getAccountName(); - this.name = source.getName(); - this.isStarred = source.getIsStarred(); - this.photoStream = source.getPhotoStream(); - this.phoneNumbers = source.getPhoneNumbers(); - this.emails = source.getEmails(); - } - @Override - SimulatorContacts.Contact.Builder setAccountType(String accountType) { - this.accountType = accountType; - return this; - } - @Override - SimulatorContacts.Contact.Builder setAccountName(String accountName) { - this.accountName = accountName; - return this; - } - @Override - SimulatorContacts.Contact.Builder setName(@Nullable String name) { - this.name = name; - return this; - } - @Override - SimulatorContacts.Contact.Builder setIsStarred(boolean isStarred) { - this.isStarred = isStarred; - return this; - } - @Override - SimulatorContacts.Contact.Builder setPhotoStream(@Nullable ByteArrayOutputStream photoStream) { - this.photoStream = photoStream; - return this; - } - @Override - SimulatorContacts.Contact.Builder setPhoneNumbers(List phoneNumbers) { - this.phoneNumbers = phoneNumbers; - return this; - } - @Override - SimulatorContacts.Contact.Builder setEmails(List emails) { - this.emails = emails; - return this; - } - @Override - SimulatorContacts.Contact build() { - String missing = ""; - if (this.accountType == null) { - missing += " accountType"; - } - if (this.accountName == null) { - missing += " accountName"; - } - if (this.isStarred == null) { - missing += " isStarred"; - } - if (this.phoneNumbers == null) { - missing += " phoneNumbers"; - } - if (this.emails == null) { - missing += " emails"; - } - if (!missing.isEmpty()) { - throw new IllegalStateException("Missing required properties:" + missing); - } - return new AutoValue_SimulatorContacts_Contact( - this.accountType, - this.accountName, - this.name, - this.isStarred, - this.photoStream, - this.phoneNumbers, - this.emails); - } - } - -} diff --git a/java/com/android/dialer/simulator/impl/AutoValue_SimulatorVoicemail_Voicemail.java b/java/com/android/dialer/simulator/impl/AutoValue_SimulatorVoicemail_Voicemail.java deleted file mode 100644 index 58934801c..000000000 --- a/java/com/android/dialer/simulator/impl/AutoValue_SimulatorVoicemail_Voicemail.java +++ /dev/null @@ -1,184 +0,0 @@ -/* - * 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.simulator.impl; - -import android.support.annotation.NonNull; -import javax.annotation.Generated; - -@Generated("com.google.auto.value.processor.AutoValueProcessor") - final class AutoValue_SimulatorVoicemail_Voicemail extends SimulatorVoicemail.Voicemail { - - private final String phoneNumber; - private final String transcription; - private final long durationSeconds; - private final long timeMillis; - private final boolean isRead; - - private AutoValue_SimulatorVoicemail_Voicemail( - String phoneNumber, - String transcription, - long durationSeconds, - long timeMillis, - boolean isRead) { - this.phoneNumber = phoneNumber; - this.transcription = transcription; - this.durationSeconds = durationSeconds; - this.timeMillis = timeMillis; - this.isRead = isRead; - } - - @NonNull - @Override - String getPhoneNumber() { - return phoneNumber; - } - - @NonNull - @Override - String getTranscription() { - return transcription; - } - - @Override - long getDurationSeconds() { - return durationSeconds; - } - - @Override - long getTimeMillis() { - return timeMillis; - } - - @Override - boolean getIsRead() { - return isRead; - } - - @Override - public String toString() { - return "Voicemail{" - + "phoneNumber=" + phoneNumber + ", " - + "transcription=" + transcription + ", " - + "durationSeconds=" + durationSeconds + ", " - + "timeMillis=" + timeMillis + ", " - + "isRead=" + isRead - + "}"; - } - - @Override - public boolean equals(Object o) { - if (o == this) { - return true; - } - if (o instanceof SimulatorVoicemail.Voicemail) { - SimulatorVoicemail.Voicemail that = (SimulatorVoicemail.Voicemail) o; - return (this.phoneNumber.equals(that.getPhoneNumber())) - && (this.transcription.equals(that.getTranscription())) - && (this.durationSeconds == that.getDurationSeconds()) - && (this.timeMillis == that.getTimeMillis()) - && (this.isRead == that.getIsRead()); - } - return false; - } - - @Override - public int hashCode() { - int h = 1; - h *= 1000003; - h ^= this.phoneNumber.hashCode(); - h *= 1000003; - h ^= this.transcription.hashCode(); - h *= 1000003; - h ^= (this.durationSeconds >>> 32) ^ this.durationSeconds; - h *= 1000003; - h ^= (this.timeMillis >>> 32) ^ this.timeMillis; - h *= 1000003; - h ^= this.isRead ? 1231 : 1237; - return h; - } - - static final class Builder extends SimulatorVoicemail.Voicemail.Builder { - private String phoneNumber; - private String transcription; - private Long durationSeconds; - private Long timeMillis; - private Boolean isRead; - Builder() { - } - private Builder(SimulatorVoicemail.Voicemail source) { - this.phoneNumber = source.getPhoneNumber(); - this.transcription = source.getTranscription(); - this.durationSeconds = source.getDurationSeconds(); - this.timeMillis = source.getTimeMillis(); - this.isRead = source.getIsRead(); - } - @Override - SimulatorVoicemail.Voicemail.Builder setPhoneNumber(String phoneNumber) { - this.phoneNumber = phoneNumber; - return this; - } - @Override - SimulatorVoicemail.Voicemail.Builder setTranscription(String transcription) { - this.transcription = transcription; - return this; - } - @Override - SimulatorVoicemail.Voicemail.Builder setDurationSeconds(long durationSeconds) { - this.durationSeconds = durationSeconds; - return this; - } - @Override - SimulatorVoicemail.Voicemail.Builder setTimeMillis(long timeMillis) { - this.timeMillis = timeMillis; - return this; - } - @Override - SimulatorVoicemail.Voicemail.Builder setIsRead(boolean isRead) { - this.isRead = isRead; - return this; - } - @Override - SimulatorVoicemail.Voicemail build() { - String missing = ""; - if (this.phoneNumber == null) { - missing += " phoneNumber"; - } - if (this.transcription == null) { - missing += " transcription"; - } - if (this.durationSeconds == null) { - missing += " durationSeconds"; - } - if (this.timeMillis == null) { - missing += " timeMillis"; - } - if (this.isRead == null) { - missing += " isRead"; - } - if (!missing.isEmpty()) { - throw new IllegalStateException("Missing required properties:" + missing); - } - return new AutoValue_SimulatorVoicemail_Voicemail( - this.phoneNumber, - this.transcription, - this.durationSeconds, - this.timeMillis, - this.isRead); - } - } - -} diff --git a/java/com/android/dialer/simulator/impl/SimulatorCallLog.java b/java/com/android/dialer/simulator/impl/SimulatorCallLog.java index 9ace047d0..f127d5603 100644 --- a/java/com/android/dialer/simulator/impl/SimulatorCallLog.java +++ b/java/com/android/dialer/simulator/impl/SimulatorCallLog.java @@ -26,7 +26,7 @@ import android.provider.CallLog.Calls; import android.support.annotation.NonNull; import android.support.annotation.WorkerThread; import com.android.dialer.common.Assert; - +import com.google.auto.value.AutoValue; import java.util.ArrayList; import java.util.concurrent.TimeUnit; @@ -96,7 +96,7 @@ final class SimulatorCallLog { } } - + @AutoValue abstract static class CallEntry { @NonNull abstract String getNumber(); @@ -121,7 +121,7 @@ final class SimulatorCallLog { return values; } - + @AutoValue.Builder abstract static class Builder { abstract Builder setNumber(@NonNull String number); diff --git a/java/com/android/dialer/simulator/impl/SimulatorContacts.java b/java/com/android/dialer/simulator/impl/SimulatorContacts.java index 89315094a..c5e25b357 100644 --- a/java/com/android/dialer/simulator/impl/SimulatorContacts.java +++ b/java/com/android/dialer/simulator/impl/SimulatorContacts.java @@ -31,7 +31,7 @@ import android.support.annotation.Nullable; import android.support.annotation.WorkerThread; import android.text.TextUtils; import com.android.dialer.common.Assert; - +import com.google.auto.value.AutoValue; import java.io.ByteArrayOutputStream; import java.util.ArrayList; import java.util.List; @@ -190,7 +190,7 @@ final class SimulatorContacts { } } - + @AutoValue abstract static class Contact { @NonNull abstract String getAccountType(); @@ -221,7 +221,7 @@ final class SimulatorContacts { .setEmails(new ArrayList<>()); } - + @AutoValue.Builder abstract static class Builder { @NonNull private final List phoneNumbers = new ArrayList<>(); @NonNull private final List emails = new ArrayList<>(); diff --git a/java/com/android/dialer/simulator/impl/SimulatorImpl.java b/java/com/android/dialer/simulator/impl/SimulatorImpl.java new file mode 100644 index 000000000..9c6826940 --- /dev/null +++ b/java/com/android/dialer/simulator/impl/SimulatorImpl.java @@ -0,0 +1,40 @@ +/* + * 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.simulator.impl; + +import android.content.Context; +import android.view.ActionProvider; +import com.android.dialer.buildtype.BuildType; +import com.android.dialer.common.LogUtil; +import com.android.dialer.simulator.Simulator; +import javax.inject.Inject; + +/** The entry point for the simulator feature. */ +final public class SimulatorImpl implements Simulator { + @Inject + public SimulatorImpl() {} + + @Override + public boolean shouldShow() { + return BuildType.get() == BuildType.BUGFOOD || LogUtil.isDebugEnabled(); + } + + @Override + public ActionProvider getActionProvider(Context context) { + return new SimulatorActionProvider(context); + } +} diff --git a/java/com/android/dialer/simulator/impl/SimulatorModule.java b/java/com/android/dialer/simulator/impl/SimulatorModule.java index 0f8ad3954..c0cca271b 100644 --- a/java/com/android/dialer/simulator/impl/SimulatorModule.java +++ b/java/com/android/dialer/simulator/impl/SimulatorModule.java @@ -16,19 +16,15 @@ package com.android.dialer.simulator.impl; -import android.content.Context; -import android.view.ActionProvider; import com.android.dialer.simulator.Simulator; +import dagger.Binds; +import dagger.Module; +import javax.inject.Singleton; -/** The entry point for the simulator module. */ -public final class SimulatorModule implements Simulator { - @Override - public boolean shouldShow() { - return true; - } - - @Override - public ActionProvider getActionProvider(Context context) { - return new SimulatorActionProvider(context); - } +/** This module provides an instance of the simulator. */ +@Module +public abstract class SimulatorModule { + @Binds + @Singleton + public abstract Simulator bindsSimulator(SimulatorImpl simulator); } diff --git a/java/com/android/dialer/simulator/impl/SimulatorVoicemail.java b/java/com/android/dialer/simulator/impl/SimulatorVoicemail.java index ffb9191dc..04de201ae 100644 --- a/java/com/android/dialer/simulator/impl/SimulatorVoicemail.java +++ b/java/com/android/dialer/simulator/impl/SimulatorVoicemail.java @@ -26,7 +26,7 @@ import android.support.annotation.WorkerThread; import android.telecom.PhoneAccountHandle; import android.telephony.TelephonyManager; import com.android.dialer.common.Assert; - +import com.google.auto.value.AutoValue; import java.util.concurrent.TimeUnit; /** Populates the device database with voicemail entries. */ @@ -105,7 +105,7 @@ final class SimulatorVoicemail { context.getContentResolver().insert(Status.buildSourceUri(context.getPackageName()), values); } - + @AutoValue abstract static class Voicemail { @NonNull abstract String getPhoneNumber(); @@ -134,7 +134,7 @@ final class SimulatorVoicemail { return values; } - + @AutoValue.Builder abstract static class Builder { abstract Builder setPhoneNumber(@NonNull String phoneNumber); diff --git a/java/com/android/dialer/telecom/TelecomUtil.java b/java/com/android/dialer/telecom/TelecomUtil.java index a11e7f77a..87ddda58b 100644 --- a/java/com/android/dialer/telecom/TelecomUtil.java +++ b/java/com/android/dialer/telecom/TelecomUtil.java @@ -23,12 +23,13 @@ import android.content.pm.PackageManager; import android.net.Uri; import android.provider.CallLog.Calls; import android.support.annotation.Nullable; +import android.support.annotation.VisibleForTesting; import android.support.v4.content.ContextCompat; import android.telecom.PhoneAccount; import android.telecom.PhoneAccountHandle; import android.telecom.TelecomManager; import android.text.TextUtils; -import android.util.Log; +import com.android.dialer.common.LogUtil; import java.util.ArrayList; import java.util.List; @@ -41,6 +42,8 @@ public class TelecomUtil { private static final String TAG = "TelecomUtil"; private static boolean sWarningLogged = false; + private static Boolean isDefaultDialerForTesting; + private static Boolean hasPermissionForTesting; public static void showInCallScreen(Context context, boolean showDialpad) { if (hasReadPhoneStatePermission(context)) { @@ -48,7 +51,7 @@ public class TelecomUtil { getTelecomManager(context).showInCallScreen(showDialpad); } catch (SecurityException e) { // Just in case - Log.w(TAG, "TelecomManager.showInCallScreen called without permission."); + LogUtil.w(TAG, "TelecomManager.showInCallScreen called without permission."); } } } @@ -59,7 +62,7 @@ public class TelecomUtil { getTelecomManager(context).silenceRinger(); } catch (SecurityException e) { // Just in case - Log.w(TAG, "TelecomManager.silenceRinger called without permission."); + LogUtil.w(TAG, "TelecomManager.silenceRinger called without permission."); } } } @@ -69,7 +72,7 @@ public class TelecomUtil { try { getTelecomManager(context).cancelMissedCallsNotification(); } catch (SecurityException e) { - Log.w(TAG, "TelecomManager.cancelMissedCalls called without permission."); + LogUtil.w(TAG, "TelecomManager.cancelMissedCalls called without permission."); } } } @@ -79,7 +82,7 @@ public class TelecomUtil { try { return getTelecomManager(context).getAdnUriForPhoneAccount(handle); } catch (SecurityException e) { - Log.w(TAG, "TelecomManager.getAdnUriForPhoneAccount called without permission."); + LogUtil.w(TAG, "TelecomManager.getAdnUriForPhoneAccount called without permission."); } } return null; @@ -95,7 +98,7 @@ public class TelecomUtil { return getTelecomManager(context).handleMmi(dialString, handle); } } catch (SecurityException e) { - Log.w(TAG, "TelecomManager.handleMmi called without permission."); + LogUtil.w(TAG, "TelecomManager.handleMmi called without permission."); } } return false; @@ -186,11 +189,17 @@ public class TelecomUtil { } private static boolean hasPermission(Context context, String permission) { + if (hasPermissionForTesting != null) { + return hasPermissionForTesting; + } return ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED; } public static boolean isDefaultDialer(Context context) { + if (isDefaultDialerForTesting != null) { + return isDefaultDialerForTesting; + } final boolean result = TextUtils.equals( context.getPackageName(), getTelecomManager(context).getDefaultDialerPackage()); @@ -199,7 +208,7 @@ public class TelecomUtil { } else { if (!sWarningLogged) { // Log only once to prevent spam. - Log.w(TAG, "Dialer is not currently set to be default dialer"); + LogUtil.w(TAG, "Dialer is not currently set to be default dialer"); sWarningLogged = true; } } @@ -209,4 +218,14 @@ public class TelecomUtil { private static TelecomManager getTelecomManager(Context context) { return (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE); } + + @VisibleForTesting(otherwise = VisibleForTesting.NONE) + public static void setIsDefaultDialerForTesting(Boolean defaultDialer) { + isDefaultDialerForTesting = defaultDialer; + } + + @VisibleForTesting(otherwise = VisibleForTesting.NONE) + public static void setHasPermissionForTesting(Boolean hasPermission) { + hasPermissionForTesting = hasPermission; + } } diff --git a/java/com/android/dialer/theme/res/drawable-hdpi/ic_block_24dp.png b/java/com/android/dialer/theme/res/drawable-hdpi/ic_block_24dp.png new file mode 100644 index 000000000..2ccc89d24 Binary files /dev/null and b/java/com/android/dialer/theme/res/drawable-hdpi/ic_block_24dp.png differ diff --git a/java/com/android/dialer/theme/res/drawable-hdpi/ic_call_arrow.png b/java/com/android/dialer/theme/res/drawable-hdpi/ic_call_arrow.png new file mode 100644 index 000000000..14a33e39f Binary files /dev/null and b/java/com/android/dialer/theme/res/drawable-hdpi/ic_call_arrow.png differ diff --git a/java/com/android/dialer/theme/res/drawable-mdpi/ic_block_24dp.png b/java/com/android/dialer/theme/res/drawable-mdpi/ic_block_24dp.png new file mode 100644 index 000000000..ec1b33f0e Binary files /dev/null and b/java/com/android/dialer/theme/res/drawable-mdpi/ic_block_24dp.png differ diff --git a/java/com/android/dialer/theme/res/drawable-mdpi/ic_call_arrow.png b/java/com/android/dialer/theme/res/drawable-mdpi/ic_call_arrow.png new file mode 100644 index 000000000..169cf2934 Binary files /dev/null and b/java/com/android/dialer/theme/res/drawable-mdpi/ic_call_arrow.png differ diff --git a/java/com/android/dialer/theme/res/drawable-xhdpi/ic_block_24dp.png b/java/com/android/dialer/theme/res/drawable-xhdpi/ic_block_24dp.png new file mode 100644 index 000000000..7aba97b65 Binary files /dev/null and b/java/com/android/dialer/theme/res/drawable-xhdpi/ic_block_24dp.png differ diff --git a/java/com/android/dialer/theme/res/drawable-xhdpi/ic_call_arrow.png b/java/com/android/dialer/theme/res/drawable-xhdpi/ic_call_arrow.png new file mode 100644 index 000000000..6f1366018 Binary files /dev/null and b/java/com/android/dialer/theme/res/drawable-xhdpi/ic_call_arrow.png differ diff --git a/java/com/android/dialer/theme/res/drawable-xxhdpi/ic_block_24dp.png b/java/com/android/dialer/theme/res/drawable-xxhdpi/ic_block_24dp.png new file mode 100644 index 000000000..fddfa54b8 Binary files /dev/null and b/java/com/android/dialer/theme/res/drawable-xxhdpi/ic_block_24dp.png differ diff --git a/java/com/android/dialer/theme/res/drawable-xxhdpi/ic_call_arrow.png b/java/com/android/dialer/theme/res/drawable-xxhdpi/ic_call_arrow.png new file mode 100644 index 000000000..0364ee015 Binary files /dev/null and b/java/com/android/dialer/theme/res/drawable-xxhdpi/ic_call_arrow.png differ diff --git a/java/com/android/dialer/theme/res/drawable-xxxhdpi/ic_block_24dp.png b/java/com/android/dialer/theme/res/drawable-xxxhdpi/ic_block_24dp.png new file mode 100644 index 000000000..0378d1bed Binary files /dev/null and b/java/com/android/dialer/theme/res/drawable-xxxhdpi/ic_block_24dp.png differ diff --git a/java/com/android/dialer/theme/res/drawable-xxxhdpi/ic_call_arrow.png b/java/com/android/dialer/theme/res/drawable-xxxhdpi/ic_call_arrow.png new file mode 100644 index 000000000..8243c2536 Binary files /dev/null and b/java/com/android/dialer/theme/res/drawable-xxxhdpi/ic_call_arrow.png differ diff --git a/java/com/android/dialer/theme/res/values/dimens.xml b/java/com/android/dialer/theme/res/values/dimens.xml index 2d11ecc84..fa750c625 100644 --- a/java/com/android/dialer/theme/res/values/dimens.xml +++ b/java/com/android/dialer/theme/res/values/dimens.xml @@ -25,4 +25,9 @@ 107dp 72dp + + 20sp + 14sp + + 4dp diff --git a/java/com/android/dialer/theme/res/values/styles.xml b/java/com/android/dialer/theme/res/values/styles.xml index ac94d0687..b5e89ff48 100644 --- a/java/com/android/dialer/theme/res/values/styles.xml +++ b/java/com/android/dialer/theme/res/values/styles.xml @@ -53,4 +53,15 @@ @color/actionbar_background_color @color/actionbar_background_color + + + + diff --git a/java/com/android/dialer/util/AndroidManifest.xml b/java/com/android/dialer/util/AndroidManifest.xml index 499df9b4e..ba22c1781 100644 --- a/java/com/android/dialer/util/AndroidManifest.xml +++ b/java/com/android/dialer/util/AndroidManifest.xml @@ -1,3 +1,19 @@ + + diff --git a/java/com/android/dialer/util/PermissionsUtil.java b/java/com/android/dialer/util/PermissionsUtil.java index 70b96dfe1..5741e734a 100644 --- a/java/com/android/dialer/util/PermissionsUtil.java +++ b/java/com/android/dialer/util/PermissionsUtil.java @@ -47,6 +47,10 @@ public class PermissionsUtil { return hasPermission(context, permission.CAMERA); } + public static boolean hasMicrophonePermissions(Context context) { + return hasPermission(context, permission.RECORD_AUDIO); + } + public static boolean hasPermission(Context context, String permission) { return ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED; diff --git a/java/com/android/dialer/util/SettingsUtil.java b/java/com/android/dialer/util/SettingsUtil.java index c61c09b6c..5043c3d56 100644 --- a/java/com/android/dialer/util/SettingsUtil.java +++ b/java/com/android/dialer/util/SettingsUtil.java @@ -69,6 +69,15 @@ public class SettingsUtil { } } } + getRingtoneName(context, handler, ringtoneUri, msg, defaultRingtone); + } + + public static void getRingtoneName(Context context, Handler handler, Uri ringtoneUri, int msg) { + getRingtoneName(context, handler, ringtoneUri, msg, false); + } + + public static void getRingtoneName( + Context context, Handler handler, Uri ringtoneUri, int msg, boolean defaultRingtone) { CharSequence summary = context.getString(R.string.ringtone_unknown); // Is it a silent ringtone? if (ringtoneUri == null) { diff --git a/java/com/android/dialer/util/ViewUtil.java b/java/com/android/dialer/util/ViewUtil.java index de08e41a7..81a32f985 100644 --- a/java/com/android/dialer/util/ViewUtil.java +++ b/java/com/android/dialer/util/ViewUtil.java @@ -27,6 +27,7 @@ import android.text.TextUtils; import android.util.TypedValue; import android.view.View; import android.view.ViewGroup; +import android.view.ViewTreeObserver.OnGlobalLayoutListener; import android.view.ViewTreeObserver.OnPreDrawListener; import android.widget.TextView; import java.util.Locale; @@ -113,6 +114,18 @@ public class ViewUtil { }); } + public static void doOnGlobalLayout(@NonNull final View view, final ViewRunnable runnable) { + view.getViewTreeObserver() + .addOnGlobalLayoutListener( + new OnGlobalLayoutListener() { + @Override + public void onGlobalLayout() { + view.getViewTreeObserver().removeOnGlobalLayoutListener(this); + runnable.run(view); + } + }); + } + /** * Returns {@code true} if animations should be disabled. * diff --git a/java/com/android/dialer/widget/MessageFragment.java b/java/com/android/dialer/widget/MessageFragment.java new file mode 100644 index 000000000..ab47f2463 --- /dev/null +++ b/java/com/android/dialer/widget/MessageFragment.java @@ -0,0 +1,172 @@ +/* + * 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.widget; + +import android.os.Bundle; +import android.support.annotation.Nullable; +import android.support.v4.app.Fragment; +import android.text.Editable; +import android.text.InputFilter; +import android.text.TextWatcher; +import android.view.LayoutInflater; +import android.view.View; +import android.view.View.OnClickListener; +import android.view.ViewGroup; +import android.widget.EditText; +import android.widget.ImageView; +import android.widget.LinearLayout; +import android.widget.TextView; +import com.android.dialer.common.Assert; +import com.android.dialer.common.FragmentUtils; + +/** Fragment used to compose call with message fragment. */ +public class MessageFragment extends Fragment implements OnClickListener, TextWatcher { + private static final String CHAR_LIMIT_KEY = "char_limit"; + private static final String SHOW_SEND_ICON_KEY = "show_send_icon"; + private static final String MESSAGE_LIST_KEY = "message_list"; + + public static final int NO_CHAR_LIMIT = -1; + + private EditText customMessage; + private ImageView sendMessage; + private TextView remainingChar; + private int charLimit; + + private static MessageFragment newInstance(Builder builder) { + MessageFragment fragment = new MessageFragment(); + Bundle args = new Bundle(); + args.putInt(CHAR_LIMIT_KEY, builder.charLimit); + args.putBoolean(SHOW_SEND_ICON_KEY, builder.showSendIcon); + args.putStringArray(MESSAGE_LIST_KEY, builder.messages); + fragment.setArguments(args); + return fragment; + } + + @Nullable + public String getMessage() { + return customMessage == null ? null : customMessage.getText().toString(); + } + + @Nullable + @Override + public View onCreateView( + LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + View view = inflater.inflate(R.layout.fragment_message, container, false); + + sendMessage = (ImageView) view.findViewById(R.id.send_message); + if (getArguments().getBoolean(SHOW_SEND_ICON_KEY, false)) { + sendMessage.setVisibility(View.VISIBLE); + sendMessage.setEnabled(false); + sendMessage.setOnClickListener(this); + } + + customMessage = (EditText) view.findViewById(R.id.custom_message); + customMessage.addTextChangedListener(this); + charLimit = getArguments().getInt(CHAR_LIMIT_KEY, NO_CHAR_LIMIT); + if (charLimit != NO_CHAR_LIMIT) { + remainingChar = (TextView) view.findViewById(R.id.remaining_characters); + remainingChar.setVisibility(View.VISIBLE); + remainingChar = (TextView) view.findViewById(R.id.remaining_characters); + remainingChar.setText("" + charLimit); + customMessage.setFilters(new InputFilter[] {new InputFilter.LengthFilter(charLimit)}); + } + + LinearLayout messageContainer = (LinearLayout) view.findViewById(R.id.message_container); + for (String message : getArguments().getStringArray(MESSAGE_LIST_KEY)) { + TextView textView = (TextView) inflater.inflate(R.layout.selectable_text_view, null); + textView.setOnClickListener(this); + textView.setText(message); + messageContainer.addView(textView); + } + return view; + } + + @Override + public void onClick(View view) { + if (view == sendMessage) { + getListener().onMessageFragmentSendMessage(customMessage.getText().toString()); + } else if (view.getId() == R.id.selectable_text_view) { + customMessage.setText(((TextView) view).getText()); + customMessage.setSelection(customMessage.getText().length()); + } else { + Assert.fail("Unknown view clicked"); + } + } + + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) {} + + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + sendMessage.setEnabled(s.length() > 0); + } + + @Override + public void afterTextChanged(Editable s) { + if (charLimit != NO_CHAR_LIMIT) { + remainingChar.setText("" + (charLimit - s.length())); + } + getListener().onMessageFragmentAfterTextChange(s.toString()); + } + + private Listener getListener() { + return FragmentUtils.getParentUnsafe(this, Listener.class); + } + + public static Builder builder() { + return new Builder(); + } + + /** Builder for {@link MessageFragment}. */ + public static class Builder { + private String[] messages; + private boolean showSendIcon; + private int charLimit = NO_CHAR_LIMIT; + + /** + * @throws NullPointerException if message is null + * @throws IllegalArgumentException if messages.length is outside the range [1,3]. + */ + public Builder setMessages(String... messages) { + // Since we only allow up to 3 messages, crash if more are set. + Assert.checkArgument(messages.length > 0 && messages.length <= 3); + this.messages = messages; + return this; + } + + public Builder showSendIcon() { + showSendIcon = true; + return this; + } + + public Builder setCharLimit(int charLimit) { + this.charLimit = charLimit; + return this; + } + + public MessageFragment build() { + return MessageFragment.newInstance(this); + } + } + + /** Interface for parent activity to implement to listen for important events. */ + public interface Listener { + void onMessageFragmentSendMessage(String message); + + void onMessageFragmentAfterTextChange(String message); + } +} diff --git a/java/com/android/dialer/widget/res/color/dialer_tint_state.xml b/java/com/android/dialer/widget/res/color/dialer_tint_state.xml new file mode 100644 index 000000000..c29f334ac --- /dev/null +++ b/java/com/android/dialer/widget/res/color/dialer_tint_state.xml @@ -0,0 +1,23 @@ + + + + + + + \ No newline at end of file diff --git a/java/com/android/dialer/widget/res/layout/fragment_message.xml b/java/com/android/dialer/widget/res/layout/fragment_message.xml new file mode 100644 index 000000000..f09c54f57 --- /dev/null +++ b/java/com/android/dialer/widget/res/layout/fragment_message.xml @@ -0,0 +1,81 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/java/com/android/dialer/widget/res/layout/selectable_text_view.xml b/java/com/android/dialer/widget/res/layout/selectable_text_view.xml new file mode 100644 index 000000000..3d120d13d --- /dev/null +++ b/java/com/android/dialer/widget/res/layout/selectable_text_view.xml @@ -0,0 +1,25 @@ + + + \ No newline at end of file diff --git a/java/com/android/dialer/widget/res/values/dimens.xml b/java/com/android/dialer/widget/res/values/dimens.xml new file mode 100644 index 000000000..6c4ea604f --- /dev/null +++ b/java/com/android/dialer/widget/res/values/dimens.xml @@ -0,0 +1,23 @@ + + + + + 16sp + 16dp + 12sp + 1dp + \ No newline at end of file diff --git a/java/com/android/dialer/widget/res/values/strings.xml b/java/com/android/dialer/widget/res/values/strings.xml new file mode 100644 index 000000000..6904c2de1 --- /dev/null +++ b/java/com/android/dialer/widget/res/values/strings.xml @@ -0,0 +1,5 @@ + + + + Write a custom message + \ No newline at end of file diff --git a/java/com/android/incallui/AnswerScreenPresenter.java b/java/com/android/incallui/AnswerScreenPresenter.java index a21876b2b..442ad260f 100644 --- a/java/com/android/incallui/AnswerScreenPresenter.java +++ b/java/com/android/incallui/AnswerScreenPresenter.java @@ -20,6 +20,7 @@ import android.content.Context; import android.support.annotation.FloatRange; import android.support.annotation.NonNull; import android.support.v4.os.UserManagerCompat; +import android.telecom.VideoProfile; import com.android.dialer.common.Assert; import com.android.dialer.common.LogUtil; import com.android.incallui.answer.protocol.AnswerScreen; @@ -71,18 +72,26 @@ public class AnswerScreenPresenter } @Override - public void onAnswer(int videoState) { + public void onAnswer(boolean answerVideoAsAudio) { if (answerScreen.isVideoUpgradeRequest()) { - call.acceptUpgradeRequest(videoState); + if (answerVideoAsAudio) { + call.getVideoTech().acceptVideoRequestAsAudio(); + } else { + call.getVideoTech().acceptVideoRequest(); + } } else { - call.answer(videoState); + if (answerVideoAsAudio) { + call.answer(VideoProfile.STATE_AUDIO_ONLY); + } else { + call.answer(); + } } } @Override public void onReject() { if (answerScreen.isVideoUpgradeRequest()) { - call.declineUpgradeRequest(); + call.getVideoTech().declineVideoRequest(); } else { call.reject(false /* rejectWithMessage */, null); } diff --git a/java/com/android/incallui/AnswerScreenPresenterStub.java b/java/com/android/incallui/AnswerScreenPresenterStub.java index fc47bf5b0..fc4e7df65 100644 --- a/java/com/android/incallui/AnswerScreenPresenterStub.java +++ b/java/com/android/incallui/AnswerScreenPresenterStub.java @@ -34,7 +34,7 @@ public class AnswerScreenPresenterStub implements AnswerScreenDelegate { public void onRejectCallWithMessage(String message) {} @Override - public void onAnswer(int videoState) {} + public void onAnswer(boolean answerVideoAsAudio) {} @Override public void onReject() {} diff --git a/java/com/android/incallui/CallButtonPresenter.java b/java/com/android/incallui/CallButtonPresenter.java index d6f4cddc9..c5c43f7aa 100644 --- a/java/com/android/incallui/CallButtonPresenter.java +++ b/java/com/android/incallui/CallButtonPresenter.java @@ -17,17 +17,13 @@ package com.android.incallui; import android.content.Context; -import android.os.Build; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.os.UserManagerCompat; import android.telecom.CallAudioState; -import android.telecom.InCallService.VideoCall; -import android.telecom.VideoProfile; import com.android.contacts.common.compat.CallCompat; import com.android.dialer.common.Assert; import com.android.dialer.common.LogUtil; -import com.android.dialer.compat.SdkVersionOverride; import com.android.dialer.logging.Logger; import com.android.dialer.logging.nano.DialerImpression; import com.android.incallui.AudioModeProvider.AudioModeListener; @@ -39,6 +35,7 @@ import com.android.incallui.InCallPresenter.InCallStateListener; import com.android.incallui.InCallPresenter.IncomingCallListener; import com.android.incallui.call.CallList; import com.android.incallui.call.DialerCall; +import com.android.incallui.call.DialerCall.CameraDirection; import com.android.incallui.call.TelecomAdapter; import com.android.incallui.call.VideoUtils; import com.android.incallui.incall.protocol.InCallButtonIds; @@ -212,6 +209,13 @@ public class CallButtonPresenter @Override public void muteClicked(boolean checked) { LogUtil.v("CallButtonPresenter", "turning on mute: " + checked); + Logger.get(mContext) + .logCallImpression( + checked + ? DialerImpression.Type.IN_CALL_SCREEN_TURN_ON_MUTE + : DialerImpression.Type.IN_CALL_SCREEN_TURN_OFF_MUTE, + mCall.getUniqueCallId(), + mCall.getTimeAddedMs()); TelecomAdapter.getInstance().mute(checked); } @@ -262,18 +266,8 @@ public class CallButtonPresenter @Override public void changeToVideoClicked() { - VideoCall videoCall = mCall.getVideoCall(); - if (videoCall == null) { - return; - } - int currVideoState = mCall.getVideoState(); - int currUnpausedVideoState = VideoUtils.getUnPausedVideoState(currVideoState); - currUnpausedVideoState |= VideoProfile.STATE_BIDIRECTIONAL; - - VideoProfile videoProfile = new VideoProfile(currUnpausedVideoState); - videoCall.sendSessionModifyRequest(videoProfile); - mCall.setSessionModificationState( - DialerCall.SESSION_MODIFICATION_STATE_WAITING_FOR_UPGRADE_TO_VIDEO_RESPONSE); + LogUtil.enterBlock("CallButtonPresenter.changeToVideoClicked"); + mCall.getVideoTech().upgradeToVideo(); } @Override @@ -300,26 +294,25 @@ public class CallButtonPresenter InCallCameraManager cameraManager = InCallPresenter.getInstance().getInCallCameraManager(); cameraManager.setUseFrontFacingCamera(useFrontFacingCamera); - VideoCall videoCall = mCall.getVideoCall(); - if (videoCall == null) { - return; - } - String cameraId = cameraManager.getActiveCameraId(); if (cameraId != null) { final int cameraDir = cameraManager.isUsingFrontFacingCamera() - ? DialerCall.VideoSettings.CAMERA_DIRECTION_FRONT_FACING - : DialerCall.VideoSettings.CAMERA_DIRECTION_BACK_FACING; - mCall.getVideoSettings().setCameraDir(cameraDir); - videoCall.setCamera(cameraId); - videoCall.requestCameraCapabilities(); + ? CameraDirection.CAMERA_DIRECTION_FRONT_FACING + : CameraDirection.CAMERA_DIRECTION_BACK_FACING; + mCall.setCameraDir(cameraDir); + mCall.getVideoTech().setCamera(cameraId); } } @Override public void toggleCameraClicked() { LogUtil.i("CallButtonPresenter.toggleCameraClicked", ""); + Logger.get(mContext) + .logCallImpression( + DialerImpression.Type.IN_CALL_SCREEN_SWAP_CAMERA, + mCall.getUniqueCallId(), + mCall.getTimeAddedMs()); switchCameraClicked( !InCallPresenter.getInstance().getInCallCameraManager().isUsingFrontFacingCamera()); } @@ -333,24 +326,19 @@ public class CallButtonPresenter @Override public void pauseVideoClicked(boolean pause) { LogUtil.i("CallButtonPresenter.pauseVideoClicked", "%s", pause ? "pause" : "unpause"); - VideoCall videoCall = mCall.getVideoCall(); - if (videoCall == null) { - return; - } - int currUnpausedVideoState = VideoUtils.getUnPausedVideoState(mCall.getVideoState()); + Logger.get(mContext) + .logCallImpression( + pause + ? DialerImpression.Type.IN_CALL_SCREEN_TURN_OFF_VIDEO + : DialerImpression.Type.IN_CALL_SCREEN_TURN_ON_VIDEO, + mCall.getUniqueCallId(), + mCall.getTimeAddedMs()); + if (pause) { - videoCall.setCamera(null); - VideoProfile videoProfile = - new VideoProfile(currUnpausedVideoState & ~VideoProfile.STATE_TX_ENABLED); - videoCall.sendSessionModifyRequest(videoProfile); + mCall.getVideoTech().stopTransmission(); } else { - InCallCameraManager cameraManager = InCallPresenter.getInstance().getInCallCameraManager(); - videoCall.setCamera(cameraManager.getActiveCameraId()); - VideoProfile videoProfile = - new VideoProfile(currUnpausedVideoState | VideoProfile.STATE_TX_ENABLED); - videoCall.sendSessionModifyRequest(videoProfile); - mCall.setSessionModificationState(DialerCall.SESSION_MODIFICATION_STATE_WAITING_FOR_RESPONSE); + mCall.getVideoTech().resumeTransmission(); } mInCallButtonUi.setVideoPaused(pause); @@ -386,7 +374,7 @@ public class CallButtonPresenter */ private void updateButtonsState(DialerCall call) { LogUtil.v("CallButtonPresenter.updateButtonsState", ""); - final boolean isVideo = VideoUtils.isVideoCall(call); + final boolean isVideo = call.isVideoCall(); // Common functionality (audio, hold, etc). // Show either HOLD or SWAP, but not both. If neither HOLD or SWAP is available: @@ -402,7 +390,7 @@ public class CallButtonPresenter final boolean showAddCall = TelecomAdapter.getInstance().canAddCall() && UserManagerCompat.isUserUnlocked(mContext); final boolean showMerge = call.can(android.telecom.Call.Details.CAPABILITY_MERGE_CONFERENCE); - final boolean showUpgradeToVideo = !isVideo && hasVideoCallCapabilities(call); + final boolean showUpgradeToVideo = !isVideo && (hasVideoCallCapabilities(call)); final boolean showDowngradeToAudio = isVideo && isDowngradeToAudioSupported(call); final boolean showMute = call.can(android.telecom.Call.Details.CAPABILITY_MUTE); @@ -427,8 +415,7 @@ public class CallButtonPresenter InCallButtonIds.BUTTON_SWITCH_CAMERA, isVideo && hasCameraPermission); mInCallButtonUi.showButton(InCallButtonIds.BUTTON_PAUSE_VIDEO, showPauseVideo); if (isVideo) { - mInCallButtonUi.setVideoPaused( - !VideoUtils.isTransmissionEnabled(call) || !hasCameraPermission); + mInCallButtonUi.setVideoPaused(!call.getVideoTech().isTransmitting() || !hasCameraPermission); } mInCallButtonUi.showButton(InCallButtonIds.BUTTON_DIALPAD, true); mInCallButtonUi.showButton(InCallButtonIds.BUTTON_MERGE, showMerge); @@ -437,12 +424,7 @@ public class CallButtonPresenter } private boolean hasVideoCallCapabilities(DialerCall call) { - if (SdkVersionOverride.getSdkVersion(Build.VERSION_CODES.M) >= Build.VERSION_CODES.M) { - return call.can(android.telecom.Call.Details.CAPABILITY_SUPPORTS_VT_LOCAL_TX) - && call.can(android.telecom.Call.Details.CAPABILITY_SUPPORTS_VT_REMOTE_RX); - } - // In L, this single flag represents both video transmitting and receiving capabilities - return call.can(android.telecom.Call.Details.CAPABILITY_SUPPORTS_VT_LOCAL_TX); + return call.getVideoTech().isAvailable(); } /** @@ -454,6 +436,7 @@ public class CallButtonPresenter * @return {@code true} if downgrading to an audio-only call from a video call is supported. */ private boolean isDowngradeToAudioSupported(DialerCall call) { + // TODO(b/33676907): If there is an RCS video share session, return true here return !call.can(CallCompat.Details.CAPABILITY_CANNOT_DOWNGRADE_VIDEO_TO_AUDIO); } diff --git a/java/com/android/incallui/CallCardPresenter.java b/java/com/android/incallui/CallCardPresenter.java index 930775772..668692d71 100644 --- a/java/com/android/incallui/CallCardPresenter.java +++ b/java/com/android/incallui/CallCardPresenter.java @@ -19,7 +19,6 @@ package com.android.incallui; import static com.android.contacts.common.compat.CallCompat.Details.PROPERTY_ENTERPRISE_CALL; import android.Manifest; -import android.app.Application; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; @@ -29,6 +28,7 @@ import android.graphics.drawable.Drawable; import android.hardware.display.DisplayManager; import android.os.BatteryManager; import android.os.Handler; +import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v4.content.ContextCompat; @@ -46,9 +46,12 @@ import com.android.contacts.common.util.ContactDisplayUtils; import com.android.dialer.common.Assert; import com.android.dialer.common.ConfigProviderBindings; import com.android.dialer.common.LogUtil; +import com.android.dialer.compat.ActivityCompat; +import com.android.dialer.enrichedcall.EnrichedCallComponent; import com.android.dialer.enrichedcall.EnrichedCallManager; import com.android.dialer.enrichedcall.Session; import com.android.dialer.multimedia.MultimediaData; +import com.android.dialer.oem.MotorolaUtils; import com.android.incallui.ContactInfoCache.ContactCacheEntry; import com.android.incallui.ContactInfoCache.ContactInfoCacheCallback; import com.android.incallui.InCallPresenter.InCallDetailsListener; @@ -58,14 +61,16 @@ import com.android.incallui.InCallPresenter.InCallStateListener; import com.android.incallui.InCallPresenter.IncomingCallListener; import com.android.incallui.call.CallList; import com.android.incallui.call.DialerCall; -import com.android.incallui.call.DialerCall.SessionModificationState; import com.android.incallui.call.DialerCallListener; +import com.android.incallui.calllocation.CallLocation; +import com.android.incallui.calllocation.CallLocationComponent; import com.android.incallui.incall.protocol.ContactPhotoType; import com.android.incallui.incall.protocol.InCallScreen; import com.android.incallui.incall.protocol.InCallScreenDelegate; import com.android.incallui.incall.protocol.PrimaryCallState; import com.android.incallui.incall.protocol.PrimaryInfo; import com.android.incallui.incall.protocol.SecondaryInfo; +import com.android.incallui.videotech.VideoTech; import java.lang.ref.WeakReference; /** @@ -116,7 +121,8 @@ public class CallCardPresenter private InCallScreen mInCallScreen; private boolean isInCallScreenReady; private boolean shouldSendAccessibilityEvent; - private final String locationModule = null; + + @NonNull private final CallLocation callLocation; private final Runnable sendAccessibilityEventRunnable = new Runnable() { @Override @@ -135,6 +141,7 @@ public class CallCardPresenter public CallCardPresenter(Context context) { LogUtil.i("CallCardController.constructor", null); mContext = Assert.isNotNull(context).getApplicationContext(); + callLocation = CallLocationComponent.get(mContext).getCallLocation(); } private static boolean hasCallSubject(DialerCall call) { @@ -175,8 +182,7 @@ public class CallCardPresenter mContactsPreferences.refreshValue(ContactsPreferences.DISPLAY_ORDER_KEY); } - EnrichedCallManager.Accessor.getInstance(((Application) mContext)) - .registerStateChangedListener(this); + EnrichedCallComponent.get(mContext).getEnrichedCallManager().registerStateChangedListener(this); // Contact search may have completed before ui is ready. if (mPrimaryContactInfo != null) { @@ -189,6 +195,11 @@ public class CallCardPresenter InCallPresenter.getInstance().addDetailsListener(this); InCallPresenter.getInstance().addInCallEventListener(this); isInCallScreenReady = true; + + // Showing the location may have been skipped if the UI wasn't ready during previous layout. + if (shouldShowLocation()) { + updatePrimaryDisplayInfo(); + } } @Override @@ -196,7 +207,8 @@ public class CallCardPresenter LogUtil.i("CallCardController.onInCallScreenUnready", null); Assert.checkState(isInCallScreenReady); - EnrichedCallManager.Accessor.getInstance(((Application) mContext)) + EnrichedCallComponent.get(mContext) + .getEnrichedCallManager() .unregisterStateChangedListener(this); // stop getting call state changes InCallPresenter.getInstance().removeListener(this); @@ -207,6 +219,8 @@ public class CallCardPresenter mPrimary.removeListener(this); } + callLocation.close(); + mPrimary = null; mPrimaryContactInfo = null; mSecondaryContactInfo = null; @@ -282,7 +296,6 @@ public class CallCardPresenter mContext, mPrimary, mPrimary.getState() == DialerCall.State.INCOMING); updatePrimaryDisplayInfo(); maybeStartSearch(mPrimary, true); - maybeClearSessionModificationState(mPrimary); } if (previousPrimary != null && mPrimary == null) { @@ -300,7 +313,6 @@ public class CallCardPresenter mContext, mSecondary, mSecondary.getState() == DialerCall.State.INCOMING); updateSecondaryDisplayInfo(); maybeStartSearch(mSecondary, false); - maybeClearSessionModificationState(mSecondary); } // Set the call state @@ -373,25 +385,18 @@ public class CallCardPresenter @Override public void onDialerCallUpgradeToVideo() {} - /** - * Handles a change to the session modification state for a call. - * - * @param sessionModificationState The new session modification state. - */ + /** Handles a change to the session modification state for a call. */ @Override - public void onDialerCallSessionModificationStateChange( - @SessionModificationState int sessionModificationState) { - LogUtil.v( - "CallCardPresenter.onDialerCallSessionModificationStateChange", - "state: " + sessionModificationState); + public void onDialerCallSessionModificationStateChange() { + LogUtil.enterBlock("CallCardPresenter.onDialerCallSessionModificationStateChange"); if (mPrimary == null) { return; } getUi() .setEndCallButtonEnabled( - sessionModificationState - != DialerCall.SESSION_MODIFICATION_STATE_RECEIVED_UPGRADE_TO_VIDEO_REQUEST, + mPrimary.getVideoTech().getSessionModificationState() + != VideoTech.SESSION_MODIFICATION_STATE_RECEIVED_UPGRADE_TO_VIDEO_REQUEST, true /* shouldAnimate */); updatePrimaryCallState(); } @@ -418,6 +423,13 @@ public class CallCardPresenter && mPrimaryContactInfo.userType == ContactsUtils.USER_TYPE_WORK); boolean isHdAudioCall = isPrimaryCallActive() && mPrimary.hasProperty(Details.PROPERTY_HIGH_DEF_AUDIO); + boolean isAttemptingHdAudioCall = + !isHdAudioCall + && !mPrimary.hasProperty(DialerCall.PROPERTY_CODEC_KNOWN) + && MotorolaUtils.shouldBlinkHdIconWhenConnectingCall(mContext); + + boolean isBusiness = mPrimaryContactInfo != null && mPrimaryContactInfo.isBusiness; + // Check for video state change and update the visibility of the contact photo. The contact // photo is hidden when the incoming video surface is shown. // The contact photo visibility can also change in setPrimary(). @@ -427,8 +439,8 @@ public class CallCardPresenter .setCallState( new PrimaryCallState( mPrimary.getState(), - mPrimary.getVideoState(), - mPrimary.getSessionModificationState(), + mPrimary.isVideoCall(), + mPrimary.getVideoTech().getSessionModificationState(), mPrimary.getDisconnectCause(), getConnectionLabel(), getCallStateIcon(), @@ -438,12 +450,14 @@ public class CallCardPresenter mPrimary.hasProperty(Details.PROPERTY_WIFI), mPrimary.isConferenceCall(), isWorkCall, + isAttemptingHdAudioCall, isHdAudioCall, !TextUtils.isEmpty(mPrimary.getLastForwardedNumber()), shouldShowContactPhoto, mPrimary.getConnectTimeMillis(), CallerInfoUtils.isVoiceMailNumber(mContext, mPrimary), - mPrimary.isRemotelyHeld())); + mPrimary.isRemotelyHeld(), + isBusiness)); InCallActivity activity = (InCallActivity) (mInCallScreen.getInCallScreenFragment().getActivity()); @@ -508,15 +522,6 @@ public class CallCardPresenter } } - private void maybeClearSessionModificationState(DialerCall call) { - @SessionModificationState int state = call.getSessionModificationState(); - if (state != DialerCall.SESSION_MODIFICATION_STATE_NO_REQUEST - && state != DialerCall.SESSION_MODIFICATION_STATE_RECEIVED_UPGRADE_TO_VIDEO_REQUEST) { - LogUtil.i("CallCardPresenter.maybeClearSessionModificationState", "clearing state"); - call.setSessionModificationState(DialerCall.SESSION_MODIFICATION_STATE_NO_REQUEST); - } - } - /** Starts a query for more contact data for the save primary and secondary calls. */ private void startContactInfoSearch( final DialerCall call, final boolean isPrimary, boolean isIncoming) { @@ -642,13 +647,17 @@ public class CallCardPresenter // DialerCall placed through a work phone account. boolean hasWorkCallProperty = mPrimary.hasProperty(PROPERTY_ENTERPRISE_CALL); - Session enrichedCallSession = - mPrimary.getNumber() == null - ? null - : EnrichedCallManager.Accessor.getInstance(((Application) mContext)) - .getSession(mPrimary.getNumber()); - MultimediaData enrichedCallMultimediaData = - enrichedCallSession == null ? null : enrichedCallSession.getMultimediaData(); + MultimediaData multimediaData = null; + if (mPrimary.getNumber() != null) { + Session enrichedCallSession = + EnrichedCallComponent.get(mContext) + .getEnrichedCallManager() + .getSession(mPrimary.getUniqueCallId(), mPrimary.getNumber()); + if (enrichedCallSession != null) { + enrichedCallSession.setUniqueDialerCallId(mPrimary.getUniqueCallId()); + multimediaData = enrichedCallSession.getMultimediaData(); + } + } if (mPrimary.isConferenceCall()) { LogUtil.v( @@ -671,7 +680,8 @@ public class CallCardPresenter false /* answeringDisconnectsOngoingCall */, shouldShowLocation(), null /* contactInfoLookupKey */, - null /* enrichedCallMultimediaData */)); + null /* enrichedCallMultimediaData */, + mPrimary.getNumberPresentation())); } else if (mPrimaryContactInfo != null) { LogUtil.v( "CallCardPresenter.updatePrimaryDisplayInfo", @@ -696,6 +706,7 @@ public class CallCardPresenter } boolean nameIsNumber = name != null && name.equals(mPrimaryContactInfo.number); + // DialerCall with caller that is a work contact. boolean isWorkContact = (mPrimaryContactInfo.userType == ContactsUtils.USER_TYPE_WORK); mInCallScreen.setPrimary( @@ -714,13 +725,52 @@ public class CallCardPresenter mPrimary.answeringDisconnectsForegroundVideoCall(), shouldShowLocation(), mPrimaryContactInfo.lookupKey, - enrichedCallMultimediaData)); + multimediaData, + mPrimary.getNumberPresentation())); } else { // Clear the primary display info. mInCallScreen.setPrimary(PrimaryInfo.createEmptyPrimaryInfo()); } - mInCallScreen.showLocationUi(null); + if (isInCallScreenReady) { + mInCallScreen.showLocationUi(getLocationFragment()); + } else { + LogUtil.i("CallCardPresenter.updatePrimaryDisplayInfo", "UI not ready, not showing location"); + } + } + + private Fragment getLocationFragment() { + if (!ConfigProviderBindings.get(mContext) + .getBoolean(CONFIG_ENABLE_EMERGENCY_LOCATION, CONFIG_ENABLE_EMERGENCY_LOCATION_DEFAULT)) { + LogUtil.i("CallCardPresenter.getLocationFragment", "disabled by config."); + return null; + } + if (!shouldShowLocation()) { + LogUtil.i("CallCardPresenter.getLocationFragment", "shouldn't show location"); + return null; + } + if (!hasLocationPermission()) { + LogUtil.i("CallCardPresenter.getLocationFragment", "no location permission."); + return null; + } + if (isBatteryTooLowForEmergencyLocation()) { + LogUtil.i("CallCardPresenter.getLocationFragment", "low battery."); + return null; + } + if (ActivityCompat.isInMultiWindowMode(mInCallScreen.getInCallScreenFragment().getActivity())) { + LogUtil.i("CallCardPresenter.getLocationFragment", "in multi-window mode"); + return null; + } + if (mPrimary.isVideoCall()) { + LogUtil.i("CallCardPresenter.getLocationFragment", "emergency video calls not supported"); + return null; + } + if (!callLocation.canGetLocation(mContext)) { + LogUtil.i("CallCardPresenter.getLocationFragment", "can't get current location"); + return null; + } + LogUtil.i("CallCardPresenter.getLocationFragment", "returning location fragment"); + return callLocation.getLocationFragment(mContext); } private boolean shouldShowLocation() { @@ -972,8 +1022,8 @@ public class CallCardPresenter || callState == DialerCall.State.INCOMING) { return false; } - if (mPrimary.getSessionModificationState() - == DialerCall.SESSION_MODIFICATION_STATE_RECEIVED_UPGRADE_TO_VIDEO_REQUEST) { + if (mPrimary.getVideoTech().getSessionModificationState() + == VideoTech.SESSION_MODIFICATION_STATE_RECEIVED_UPGRADE_TO_VIDEO_REQUEST) { return false; } return true; diff --git a/java/com/android/incallui/CallerInfoAsyncQuery.java b/java/com/android/incallui/CallerInfoAsyncQuery.java index f8d7ac65a..d620d4705 100644 --- a/java/com/android/incallui/CallerInfoAsyncQuery.java +++ b/java/com/android/incallui/CallerInfoAsyncQuery.java @@ -55,7 +55,7 @@ import java.util.Arrays; public class CallerInfoAsyncQuery { /** Interface for a CallerInfoAsyncQueryHandler result return. */ - public interface OnQueryCompleteListener { + interface OnQueryCompleteListener { /** Called when the query is complete. */ @MainThread @@ -85,7 +85,7 @@ public class CallerInfoAsyncQuery { private CallerInfoAsyncQuery() {} @RequiresPermission(Manifest.permission.READ_CONTACTS) - public static void startQuery( + static void startQuery( final int token, final Context context, final CallerInfo info, @@ -99,7 +99,7 @@ public class CallerInfoAsyncQuery { new OnQueryCompleteListener() { @Override public void onQueryComplete(int token, Object cookie, CallerInfo ci) { - Log.d(LOG_TAG, "contactsProviderQueryCompleteListener done"); + Log.d(LOG_TAG, "contactsProviderQueryCompleteListener onQueryComplete"); // If there are no other directory queries, make sure that the listener is // notified of this result. see b/27621628 if ((ci != null && ci.contactExists) @@ -112,6 +112,7 @@ public class CallerInfoAsyncQuery { @Override public void onDataLoaded(int token, Object cookie, CallerInfo ci) { + Log.d(LOG_TAG, "contactsProviderQueryCompleteListener onDataLoaded"); listener.onDataLoaded(token, cookie, ci); } }; @@ -270,9 +271,9 @@ public class CallerInfoAsyncQuery { /* Directory lookup related code - END */ /** Simple exception used to communicate problems with the query pool. */ - public static class QueryPoolException extends SQLException { + private static class QueryPoolException extends SQLException { - public QueryPoolException(String error) { + QueryPoolException(String error) { super(error); } } @@ -337,7 +338,7 @@ public class CallerInfoAsyncQuery { } } - public OnQueryCompleteListener newListener(long directoryId) { + OnQueryCompleteListener newListener(long directoryId) { return new DirectoryQueryCompleteListener(directoryId); } @@ -351,11 +352,13 @@ public class CallerInfoAsyncQuery { @Override public void onDataLoaded(int token, Object cookie, CallerInfo ci) { + Log.d(LOG_TAG, "DirectoryQueryCompleteListener.onDataLoaded"); mListener.onDataLoaded(token, cookie, ci); } @Override public void onQueryComplete(int token, Object cookie, CallerInfo ci) { + Log.d(LOG_TAG, "DirectoryQueryCompleteListener.onQueryComplete"); onDirectoryQueryComplete(token, cookie, ci, mDirectoryId); } } @@ -446,7 +449,7 @@ public class CallerInfoAsyncQuery { mCallerInfo = null; } - protected void updateData(int token, Object cookie, Cursor cursor) { + void updateData(int token, Object cookie, Cursor cursor) { try { Log.d(this, "##### updateData() ##### for token: " + token); @@ -549,9 +552,9 @@ public class CallerInfoAsyncQuery { * times before the query is complete. All accesses (listeners) must be queued up and informed * in order when the query is complete. */ - protected class CallerInfoWorkerHandler extends WorkerHandler { + class CallerInfoWorkerHandler extends WorkerHandler { - public CallerInfoWorkerHandler(Looper looper) { + CallerInfoWorkerHandler(Looper looper) { super(looper); } @@ -624,7 +627,7 @@ public class CallerInfoAsyncQuery { case EVENT_ADD_LISTENER: updateData(msg.arg1, cw, (Cursor) args.result); break; - default: + default: // fall out } Message reply = args.handler.obtainMessage(msg.what); reply.obj = args; diff --git a/java/com/android/incallui/CallerInfoUtils.java b/java/com/android/incallui/CallerInfoUtils.java index 9f57fba65..7c14533bb 100644 --- a/java/com/android/incallui/CallerInfoUtils.java +++ b/java/com/android/incallui/CallerInfoUtils.java @@ -22,6 +22,7 @@ import android.content.Loader; import android.content.Loader.OnLoadCompleteListener; import android.content.pm.PackageManager; import android.net.Uri; +import android.support.annotation.NonNull; import android.support.v4.content.ContextCompat; import android.telecom.PhoneAccount; import android.telecom.TelecomManager; @@ -53,7 +54,7 @@ public class CallerInfoUtils { * OnQueryCompleteListener (which contains information about the phone number label, user's name, * etc). */ - public static CallerInfo getCallerInfoForCall( + static CallerInfo getCallerInfoForCall( Context context, DialerCall call, Object cookie, @@ -81,7 +82,7 @@ public class CallerInfoUtils { return info; } - public static CallerInfo buildCallerInfo(Context context, DialerCall call) { + static CallerInfo buildCallerInfo(Context context, DialerCall call) { CallerInfo info = new CallerInfo(); // Store CNAP information retrieved from the Connection (we want to do this @@ -91,6 +92,7 @@ public class CallerInfoUtils { info.numberPresentation = call.getNumberPresentation(); info.namePresentation = call.getCnapNamePresentation(); info.callSubject = call.getCallSubject(); + info.contactExists = false; String number = call.getNumber(); if (!TextUtils.isEmpty(number)) { @@ -109,9 +111,7 @@ public class CallerInfoUtils { // Because the InCallUI is immediately launched before the call is connected, occasionally // a voicemail call will be passed to InCallUI as a "voicemail:" URI without a number. // This call should still be handled as a voicemail call. - if ((call.getHandle() != null - && PhoneAccount.SCHEME_VOICEMAIL.equals(call.getHandle().getScheme())) - || isVoiceMailNumber(context, call)) { + if (isVoiceMailNumber(context, call)) { info.markAsVoiceMail(context); } @@ -145,11 +145,17 @@ public class CallerInfoUtils { return cacheInfo; } - public static boolean isVoiceMailNumber(Context context, DialerCall call) { + public static boolean isVoiceMailNumber(Context context, @NonNull DialerCall call) { + if (call.getHandle() != null + && PhoneAccount.SCHEME_VOICEMAIL.equals(call.getHandle().getScheme())) { + return true; + } + if (ContextCompat.checkSelfPermission(context, permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) { return false; } + return TelecomUtil.isVoicemailNumber(context, call.getAccountHandle(), call.getNumber()); } diff --git a/java/com/android/incallui/ContactInfoCache.java b/java/com/android/incallui/ContactInfoCache.java index 4d4d94a17..c4e25e700 100644 --- a/java/com/android/incallui/ContactInfoCache.java +++ b/java/com/android/incallui/ContactInfoCache.java @@ -35,6 +35,7 @@ import android.support.annotation.NonNull; import android.support.annotation.WorkerThread; import android.support.v4.os.UserManagerCompat; import android.telecom.TelecomManager; +import android.telephony.PhoneNumberUtils; import android.text.TextUtils; import android.util.ArrayMap; import android.util.ArraySet; @@ -74,10 +75,11 @@ public class ContactInfoCache implements OnImageLoadCompleteListener { private final PhoneNumberService mPhoneNumberService; // Cache info map needs to be thread-safe since it could be modified by both main thread and // worker thread. - private final Map mInfoMap = new ConcurrentHashMap<>(); + private final ConcurrentHashMap mInfoMap = new ConcurrentHashMap<>(); private final Map> mCallBacks = new ArrayMap<>(); private Drawable mDefaultContactPhotoDrawable; private Drawable mConferencePhotoDrawable; + private int mQueryId; private ContactInfoCache(Context context) { mContext = context; @@ -91,7 +93,7 @@ public class ContactInfoCache implements OnImageLoadCompleteListener { return sCache; } - public static ContactCacheEntry buildCacheEntryFromCall( + static ContactCacheEntry buildCacheEntryFromCall( Context context, DialerCall call, boolean isIncoming) { final ContactCacheEntry entry = new ContactCacheEntry(); @@ -103,7 +105,7 @@ public class ContactInfoCache implements OnImageLoadCompleteListener { } /** Populate a cache entry from a call (which got converted into a caller info). */ - public static void populateCacheEntry( + private static void populateCacheEntry( @NonNull Context context, @NonNull CallerInfo info, @NonNull ContactCacheEntry cce, @@ -153,7 +155,7 @@ public class ContactInfoCache implements OnImageLoadCompleteListener { // (Typically, we promote the phone number up to the "name" slot // onscreen, and possibly display a descriptive string in the // "number" slot.) - if (TextUtils.isEmpty(number)) { + if (TextUtils.isEmpty(number) && TextUtils.isEmpty(info.cnapName)) { // No name *or* number! Display a generic "unknown" string // (or potentially some other default based on the presentation.) displayName = getPresentationString(context, presentation, info.callSubject); @@ -236,6 +238,7 @@ public class ContactInfoCache implements OnImageLoadCompleteListener { cce.label = label; cce.isSipCall = isSipCall; cce.userType = info.userType; + cce.originalPhoneNumber = info.phoneNumber; if (info.contactExists) { cce.contactLookupResult = ContactLookupResult.Type.LOCAL_CONTACT; @@ -261,11 +264,11 @@ public class ContactInfoCache implements OnImageLoadCompleteListener { return name; } - public ContactCacheEntry getInfo(String callId) { + ContactCacheEntry getInfo(String callId) { return mInfoMap.get(callId); } - public void maybeInsertCnapInformationIntoCache( + void maybeInsertCnapInformationIntoCache( Context context, final DialerCall call, final CallerInfo info) { final CachedNumberLookupService cachedNumberLookupService = PhoneNumberCache.get(context).getCachedNumberLookupService(); @@ -331,8 +334,13 @@ public class ContactInfoCache implements OnImageLoadCompleteListener { final ContactCacheEntry cacheEntry = mInfoMap.get(callId); Set callBacks = mCallBacks.get(callId); - // If we have a previously obtained intermediate result return that now - if (cacheEntry != null) { + // We need to force a new query if phone number has changed. + boolean forceQuery = needForceQuery(call, cacheEntry); + Log.d(TAG, "findInfo: callId = " + callId + "; forceQuery = " + forceQuery); + + // If we have a previously obtained intermediate result return that now except needs + // force query. + if (cacheEntry != null && !forceQuery) { Log.d( TAG, "Contact lookup. In memory cache hit; lookup " @@ -346,14 +354,19 @@ public class ContactInfoCache implements OnImageLoadCompleteListener { // If the entry already exists, add callback if (callBacks != null) { + Log.d(TAG, "Another query is in progress, add callback only."); callBacks.add(callback); - return; + if (!forceQuery) { + Log.d(TAG, "No need to query again, just return and wait for existing query to finish"); + return; + } + } else { + Log.d(TAG, "Contact lookup. In memory cache miss; searching provider."); + // New lookup + callBacks = new ArraySet<>(); + callBacks.add(callback); + mCallBacks.put(callId, callBacks); } - Log.d(TAG, "Contact lookup. In memory cache miss; searching provider."); - // New lookup - callBacks = new ArraySet<>(); - callBacks.add(callback); - mCallBacks.put(callId, callBacks); /** * Performs a query for caller information. Save any immediate data we get from the query. An @@ -361,25 +374,47 @@ public class ContactInfoCache implements OnImageLoadCompleteListener { * such as those for voicemail and emergency call information, will not perform an additional * asynchronous query. */ + final CallerInfoQueryToken queryToken = new CallerInfoQueryToken(mQueryId, callId); + mQueryId++; final CallerInfo callerInfo = CallerInfoUtils.getCallerInfoForCall( mContext, call, new DialerCallCookieWrapper(callId, call.getNumberPresentation()), - new FindInfoCallback(isIncoming)); + new FindInfoCallback(isIncoming, queryToken)); - updateCallerInfoInCacheOnAnyThread( - callId, call.getNumberPresentation(), callerInfo, isIncoming, false); - sendInfoNotifications(callId, mInfoMap.get(callId)); + if (cacheEntry != null) { + // We should not override the old cache item until the new query is + // back. We should only update the queryId. Otherwise, we may see + // flicker of the name and image (old cache -> new cache before query + // -> new cache after query) + cacheEntry.queryId = queryToken.mQueryId; + Log.d(TAG, "There is an existing cache. Do not override until new query is back"); + } else { + ContactCacheEntry initialCacheEntry = + updateCallerInfoInCacheOnAnyThread( + callId, call.getNumberPresentation(), callerInfo, isIncoming, false, queryToken); + sendInfoNotifications(callId, initialCacheEntry); + } } @AnyThread - private void updateCallerInfoInCacheOnAnyThread( + private ContactCacheEntry updateCallerInfoInCacheOnAnyThread( String callId, int numberPresentation, CallerInfo callerInfo, boolean isIncoming, - boolean didLocalLookup) { + boolean didLocalLookup, + CallerInfoQueryToken queryToken) { + Log.d( + TAG, + "updateCallerInfoInCacheOnAnyThread: callId = " + + callId + + "; queryId = " + + queryToken.mQueryId + + "; didLocalLookup = " + + didLocalLookup); + int presentationMode = numberPresentation; if (callerInfo.contactExists || callerInfo.isEmergencyNumber() @@ -387,38 +422,57 @@ public class ContactInfoCache implements OnImageLoadCompleteListener { presentationMode = TelecomManager.PRESENTATION_ALLOWED; } - synchronized (mInfoMap) { - ContactCacheEntry cacheEntry = mInfoMap.get(callId); - // Ensure we always have a cacheEntry. Replace the existing entry if - // it has no name or if we found a local contact. - if (cacheEntry == null - || TextUtils.isEmpty(cacheEntry.namePrimary) - || callerInfo.contactExists) { - cacheEntry = buildEntry(mContext, callerInfo, presentationMode, isIncoming); - mInfoMap.put(callId, cacheEntry); - } - if (didLocalLookup) { - // Before issuing a request for more data from other services, we only check that the - // contact wasn't found in the local DB. We don't check the if the cache entry already - // has a name because we allow overriding cnap data with data from other services. - if (!callerInfo.contactExists && mPhoneNumberService != null) { - Log.d(TAG, "Contact lookup. Local contacts miss, checking remote"); - final PhoneNumberServiceListener listener = new PhoneNumberServiceListener(callId); - mPhoneNumberService.getPhoneNumberInfo(cacheEntry.number, listener, listener, isIncoming); - } else if (cacheEntry.displayPhotoUri != null) { - Log.d(TAG, "Contact lookup. Local contact found, starting image load"); - // Load the image with a callback to update the image state. - // When the load is finished, onImageLoadComplete() will be called. - cacheEntry.hasPhotoToLoad = true; - ContactsAsyncHelper.startObtainPhotoAsync( - TOKEN_UPDATE_PHOTO_FOR_CALL_STATE, - mContext, - cacheEntry.displayPhotoUri, - ContactInfoCache.this, - callId); + // We always replace the entry. The only exception is the same photo case. + ContactCacheEntry cacheEntry = buildEntry(mContext, callerInfo, presentationMode, isIncoming); + cacheEntry.queryId = queryToken.mQueryId; + + ContactCacheEntry existingCacheEntry = mInfoMap.get(callId); + Log.d(TAG, "Existing cacheEntry in hashMap " + existingCacheEntry); + + if (didLocalLookup) { + // Before issuing a request for more data from other services, we only check that the + // contact wasn't found in the local DB. We don't check the if the cache entry already + // has a name because we allow overriding cnap data with data from other services. + if (!callerInfo.contactExists && mPhoneNumberService != null) { + Log.d(TAG, "Contact lookup. Local contacts miss, checking remote"); + final PhoneNumberServiceListener listener = + new PhoneNumberServiceListener(callId, queryToken.mQueryId); + cacheEntry.hasPendingQuery = true; + mPhoneNumberService.getPhoneNumberInfo(cacheEntry.number, listener, listener, isIncoming); + } else if (cacheEntry.displayPhotoUri != null) { + // When the difference between 2 numbers is only the prefix (e.g. + or IDD), + // we will still trigger force query so that the number can be updated on + // the calling screen. We need not query the image again if the previous + // query already has the image to avoid flickering. + if (existingCacheEntry != null + && existingCacheEntry.displayPhotoUri != null + && existingCacheEntry.displayPhotoUri.equals(cacheEntry.displayPhotoUri) + && existingCacheEntry.photo != null) { + Log.d(TAG, "Same picture. Do not need start image load."); + cacheEntry.photo = existingCacheEntry.photo; + cacheEntry.photoType = existingCacheEntry.photoType; + return cacheEntry; } + + Log.d(TAG, "Contact lookup. Local contact found, starting image load"); + // Load the image with a callback to update the image state. + // When the load is finished, onImageLoadComplete() will be called. + cacheEntry.hasPendingQuery = true; + ContactsAsyncHelper.startObtainPhotoAsync( + TOKEN_UPDATE_PHOTO_FOR_CALL_STATE, + mContext, + cacheEntry.displayPhotoUri, + ContactInfoCache.this, + queryToken); } + Log.d(TAG, "put entry into map: " + cacheEntry); + mInfoMap.put(callId, cacheEntry); + } else { + // Don't overwrite if there is existing cache. + Log.d(TAG, "put entry into map if not exists: " + cacheEntry); + mInfoMap.putIfAbsent(callId, cacheEntry); } + return cacheEntry; } /** @@ -429,35 +483,42 @@ public class ContactInfoCache implements OnImageLoadCompleteListener { @Override public void onImageLoaded(int token, Drawable photo, Bitmap photoIcon, Object cookie) { Assert.isWorkerThread(); + CallerInfoQueryToken myCookie = (CallerInfoQueryToken) cookie; + final String callId = myCookie.mCallId; + final int queryId = myCookie.mQueryId; + if (!isWaitingForThisQuery(callId, queryId)) { + return; + } loadImage(photo, photoIcon, cookie); } private void loadImage(Drawable photo, Bitmap photoIcon, Object cookie) { - Log.d(this, "Image load complete with context: ", mContext); + Log.d(TAG, "Image load complete with context: ", mContext); // TODO: may be nice to update the image view again once the newer one // is available on contacts database. - String callId = (String) cookie; + CallerInfoQueryToken myCookie = (CallerInfoQueryToken) cookie; + final String callId = myCookie.mCallId; ContactCacheEntry entry = mInfoMap.get(callId); if (entry == null) { - Log.e(this, "Image Load received for empty search entry."); + Log.e(TAG, "Image Load received for empty search entry."); clearCallbacks(callId); return; } - Log.d(this, "setting photo for entry: ", entry); + Log.d(TAG, "setting photo for entry: ", entry); // Conference call icons are being handled in CallCardPresenter. if (photo != null) { - Log.v(this, "direct drawable: ", photo); + Log.v(TAG, "direct drawable: ", photo); entry.photo = photo; entry.photoType = ContactPhotoType.CONTACT; } else if (photoIcon != null) { - Log.v(this, "photo icon: ", photoIcon); + Log.v(TAG, "photo icon: ", photoIcon); entry.photo = new BitmapDrawable(mContext.getResources(), photoIcon); entry.photoType = ContactPhotoType.CONTACT; } else { - Log.v(this, "unknown photo"); + Log.v(TAG, "unknown photo"); entry.photo = null; entry.photoType = ContactPhotoType.DEFAULT_PLACEHOLDER; } @@ -471,9 +532,13 @@ public class ContactInfoCache implements OnImageLoadCompleteListener { @Override public void onImageLoadComplete(int token, Drawable photo, Bitmap photoIcon, Object cookie) { Assert.isMainThread(); - String callId = (String) cookie; - ContactCacheEntry entry = mInfoMap.get(callId); - sendImageNotifications(callId, entry); + CallerInfoQueryToken myCookie = (CallerInfoQueryToken) cookie; + final String callId = myCookie.mCallId; + final int queryId = myCookie.mQueryId; + if (!isWaitingForThisQuery(callId, queryId)) { + return; + } + sendImageNotifications(callId, mInfoMap.get(callId)); clearCallbacks(callId); } @@ -482,6 +547,7 @@ public class ContactInfoCache implements OnImageLoadCompleteListener { public void clearCache() { mInfoMap.clear(); mCallBacks.clear(); + mQueryId = 0; } private ContactCacheEntry buildEntry( @@ -500,9 +566,6 @@ public class ContactInfoCache implements OnImageLoadCompleteListener { cce.photo = getDefaultContactPhotoDrawable(); cce.photoType = ContactPhotoType.DEFAULT_PLACEHOLDER; } - } else if (info.contactDisplayPhotoUri == null) { - cce.photo = getDefaultContactPhotoDrawable(); - cce.photoType = ContactPhotoType.DEFAULT_PLACEHOLDER; } else { cce.displayPhotoUri = info.contactDisplayPhotoUri; cce.photo = null; @@ -528,7 +591,9 @@ public class ContactInfoCache implements OnImageLoadCompleteListener { } /** Sends the updated information to call the callbacks for the entry. */ + @MainThread private void sendInfoNotifications(String callId, ContactCacheEntry entry) { + Assert.isMainThread(); final Set callBacks = mCallBacks.get(callId); if (callBacks != null) { for (ContactInfoCacheCallback callBack : callBacks) { @@ -537,7 +602,9 @@ public class ContactInfoCache implements OnImageLoadCompleteListener { } } + @MainThread private void sendImageNotifications(String callId, ContactCacheEntry entry) { + Assert.isMainThread(); final Set callBacks = mCallBacks.get(callId); if (callBacks != null && entry.photo != null) { for (ContactInfoCacheCallback callBack : callBacks) { @@ -583,21 +650,26 @@ public class ContactInfoCache implements OnImageLoadCompleteListener { public String location; public String label; public Drawable photo; - @ContactPhotoType public int photoType; - public boolean isSipCall; + @ContactPhotoType int photoType; + boolean isSipCall; // Note in cache entry whether this is a pending async loading action to know whether to // wait for its callback or not. - public boolean hasPhotoToLoad; + boolean hasPendingQuery; /** This will be used for the "view" notification. */ public Uri contactUri; /** Either a display photo or a thumbnail URI. */ - public Uri displayPhotoUri; + Uri displayPhotoUri; public Uri lookupUri; // Sent to NotificationMananger public String lookupKey; public int contactLookupResult = ContactLookupResult.Type.NOT_FOUND; public long userType = ContactsUtils.USER_TYPE_CURRENT; - public Uri contactRingtoneUri; + Uri contactRingtoneUri; + /** Query id to identify the query session. */ + int queryId; + /** The phone number without any changes to display to the user (ex: cnap...) */ + String originalPhoneNumber; + boolean isBusiness; @Override public String toString() { @@ -631,6 +703,10 @@ public class ContactInfoCache implements OnImageLoadCompleteListener { + userType + ", contactRingtoneUri=" + contactRingtoneUri + + ", queryId=" + + queryId + + ", originalPhoneNumber=" + + originalPhoneNumber + '}'; } } @@ -648,16 +724,22 @@ public class ContactInfoCache implements OnImageLoadCompleteListener { private class FindInfoCallback implements OnQueryCompleteListener { private final boolean mIsIncoming; + private final CallerInfoQueryToken mQueryToken; - public FindInfoCallback(boolean isIncoming) { + public FindInfoCallback(boolean isIncoming, CallerInfoQueryToken queryToken) { mIsIncoming = isIncoming; + mQueryToken = queryToken; } @Override public void onDataLoaded(int token, Object cookie, CallerInfo ci) { Assert.isWorkerThread(); DialerCallCookieWrapper cw = (DialerCallCookieWrapper) cookie; - updateCallerInfoInCacheOnAnyThread(cw.callId, cw.numberPresentation, ci, mIsIncoming, true); + if (!isWaitingForThisQuery(cw.callId, mQueryToken.mQueryId)) { + return; + } + updateCallerInfoInCacheOnAnyThread( + cw.callId, cw.numberPresentation, ci, mIsIncoming, true, mQueryToken); } @Override @@ -665,6 +747,9 @@ public class ContactInfoCache implements OnImageLoadCompleteListener { Assert.isMainThread(); DialerCallCookieWrapper cw = (DialerCallCookieWrapper) cookie; String callId = cw.callId; + if (!isWaitingForThisQuery(cw.callId, mQueryToken.mQueryId)) { + return; + } ContactCacheEntry cacheEntry = mInfoMap.get(callId); // This may happen only when InCallPresenter attempt to cleanup. if (cacheEntry == null) { @@ -673,7 +758,7 @@ public class ContactInfoCache implements OnImageLoadCompleteListener { return; } sendInfoNotifications(callId, cacheEntry); - if (!cacheEntry.hasPhotoToLoad) { + if (!cacheEntry.hasPendingQuery) { if (callerInfo.contactExists) { Log.d(TAG, "Contact lookup done. Local contact found, no image."); } else { @@ -691,13 +776,20 @@ public class ContactInfoCache implements OnImageLoadCompleteListener { implements PhoneNumberService.NumberLookupListener, PhoneNumberService.ImageLookupListener { private final String mCallId; + private final int mQueryIdOfRemoteLookup; - PhoneNumberServiceListener(String callId) { + PhoneNumberServiceListener(String callId, int queryId) { mCallId = callId; + mQueryIdOfRemoteLookup = queryId; } @Override public void onPhoneNumberInfoComplete(final PhoneNumberService.PhoneNumberInfo info) { + Log.d(TAG, "PhoneNumberServiceListener.onPhoneNumberInfoComplete"); + if (!isWaitingForThisQuery(mCallId, mQueryIdOfRemoteLookup)) { + return; + } + // If we got a miss, this is the end of the lookup pipeline, // so clear the callbacks and return. if (info == null) { @@ -705,11 +797,11 @@ public class ContactInfoCache implements OnImageLoadCompleteListener { clearCallbacks(mCallId); return; } - ContactCacheEntry entry = new ContactCacheEntry(); entry.namePrimary = info.getDisplayName(); entry.number = info.getNumber(); entry.contactLookupResult = info.getLookupSource(); + entry.isBusiness = info.isBusiness(); final int type = info.getPhoneType(); final String label = info.getPhoneLabel(); if (type == Phone.TYPE_CUSTOM) { @@ -718,33 +810,32 @@ public class ContactInfoCache implements OnImageLoadCompleteListener { final CharSequence typeStr = Phone.getTypeLabel(mContext.getResources(), type, label); entry.label = typeStr == null ? null : typeStr.toString(); } - synchronized (mInfoMap) { - final ContactCacheEntry oldEntry = mInfoMap.get(mCallId); - if (oldEntry != null) { - // Location is only obtained from local lookup so persist - // the value for remote lookups. Once we have a name this - // field is no longer used; it is persisted here in case - // the UI is ever changed to use it. - entry.location = oldEntry.location; - // Contact specific ringtone is obtained from local lookup. - entry.contactRingtoneUri = oldEntry.contactRingtoneUri; - } - - // If no image and it's a business, switch to using the default business avatar. - if (info.getImageUrl() == null && info.isBusiness()) { - Log.d(TAG, "Business has no image. Using default."); - entry.photo = mContext.getResources().getDrawable(R.drawable.img_business); - entry.photoType = ContactPhotoType.BUSINESS; - } + final ContactCacheEntry oldEntry = mInfoMap.get(mCallId); + if (oldEntry != null) { + // Location is only obtained from local lookup so persist + // the value for remote lookups. Once we have a name this + // field is no longer used; it is persisted here in case + // the UI is ever changed to use it. + entry.location = oldEntry.location; + // Contact specific ringtone is obtained from local lookup. + entry.contactRingtoneUri = oldEntry.contactRingtoneUri; + } - mInfoMap.put(mCallId, entry); + // If no image and it's a business, switch to using the default business avatar. + if (info.getImageUrl() == null && info.isBusiness()) { + Log.d(TAG, "Business has no image. Using default."); + entry.photo = mContext.getResources().getDrawable(R.drawable.img_business); + entry.photoType = ContactPhotoType.BUSINESS; } + + Log.d(TAG, "put entry into map: " + entry); + mInfoMap.put(mCallId, entry); sendInfoNotifications(mCallId, entry); - entry.hasPhotoToLoad = info.getImageUrl() != null; + entry.hasPendingQuery = info.getImageUrl() != null; // If there is no image then we should not expect another callback. - if (!entry.hasPhotoToLoad) { + if (!entry.hasPendingQuery) { // We're done, so clear callbacks clearCallbacks(mCallId); } @@ -752,8 +843,59 @@ public class ContactInfoCache implements OnImageLoadCompleteListener { @Override public void onImageFetchComplete(Bitmap bitmap) { - loadImage(null, bitmap, mCallId); - onImageLoadComplete(TOKEN_UPDATE_PHOTO_FOR_CALL_STATE, null, bitmap, mCallId); + Log.d(TAG, "PhoneNumberServiceListener.onImageFetchComplete"); + if (!isWaitingForThisQuery(mCallId, mQueryIdOfRemoteLookup)) { + return; + } + CallerInfoQueryToken queryToken = new CallerInfoQueryToken(mQueryIdOfRemoteLookup, mCallId); + loadImage(null, bitmap, queryToken); + onImageLoadComplete(TOKEN_UPDATE_PHOTO_FOR_CALL_STATE, null, bitmap, queryToken); + } + } + + private boolean needForceQuery(DialerCall call, ContactCacheEntry cacheEntry) { + if (call == null || call.isConferenceCall()) { + return false; + } + + String newPhoneNumber = PhoneNumberUtils.stripSeparators(call.getNumber()); + if (cacheEntry == null) { + // No info in the map yet so it is the 1st query + Log.d(TAG, "needForceQuery: first query"); + return true; + } + String oldPhoneNumber = PhoneNumberUtils.stripSeparators(cacheEntry.originalPhoneNumber); + + if (!TextUtils.equals(oldPhoneNumber, newPhoneNumber)) { + Log.d(TAG, "phone number has changed: " + oldPhoneNumber + " -> " + newPhoneNumber); + return true; + } + + return false; + } + + private static final class CallerInfoQueryToken { + final int mQueryId; + final String mCallId; + + CallerInfoQueryToken(int queryId, String callId) { + mQueryId = queryId; + mCallId = callId; + } + } + + /** Check if the queryId in the cached map is the same as the one from query result. */ + private boolean isWaitingForThisQuery(String callId, int queryId) { + final ContactCacheEntry existingCacheEntry = mInfoMap.get(callId); + if (existingCacheEntry == null) { + // This might happen if lookup on background thread comes back before the initial entry is + // created. + Log.d(TAG, "Cached entry is null."); + return true; + } else { + int waitingQueryId = existingCacheEntry.queryId; + Log.d(TAG, "waitingQueryId = " + waitingQueryId + "; queryId = " + queryId); + return waitingQueryId == queryId; } } } diff --git a/java/com/android/incallui/ExternalCallNotifier.java b/java/com/android/incallui/ExternalCallNotifier.java index 466e12a6d..6ec94a631 100644 --- a/java/com/android/incallui/ExternalCallNotifier.java +++ b/java/com/android/incallui/ExternalCallNotifier.java @@ -41,6 +41,8 @@ import com.android.contacts.common.compat.CallCompat; import com.android.contacts.common.preference.ContactsPreferences; import com.android.contacts.common.util.BitmapUtil; import com.android.contacts.common.util.ContactDisplayUtils; +import com.android.dialer.notification.NotificationChannelManager; +import com.android.dialer.notification.NotificationChannelManager.Channel; import com.android.incallui.call.DialerCall; import com.android.incallui.call.DialerCallDelegate; import com.android.incallui.call.ExternalCallList; @@ -57,9 +59,9 @@ import java.util.Map; public class ExternalCallNotifier implements ExternalCallList.ExternalCallListener { /** Tag used with the notification manager to uniquely identify external call notifications. */ - private static final String NOTIFICATION_TAG = "EXTERNAL_CALL"; + private static final int NOTIFICATION_ID = R.id.notification_external_call; - private static final int SUMMARY_ID = -1; + private static final String NOTIFICATION_GROUP = "ExternalCallNotifier"; private final Context mContext; private final ContactInfoCache mContactInfoCache; private Map mNotifications = new ArrayMap<>(); @@ -186,14 +188,15 @@ public class ExternalCallNotifier implements ExternalCallList.ExternalCallListen NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); - notificationManager.cancel(NOTIFICATION_TAG, mNotifications.get(call).getNotificationId()); + notificationManager.cancel( + String.valueOf(mNotifications.get(call).getNotificationId()), NOTIFICATION_ID); mNotifications.remove(call); if (mShowingSummary && mNotifications.size() <= 1) { // Where a summary notification is showing and there is now not enough notifications to // necessitate a summary, cancel the summary. - notificationManager.cancel(NOTIFICATION_TAG, SUMMARY_ID); + notificationManager.cancel(NOTIFICATION_GROUP, NOTIFICATION_ID); mShowingSummary = false; // If there is still a single call requiring a notification, re-post the notification as a @@ -234,7 +237,7 @@ public class ExternalCallNotifier implements ExternalCallList.ExternalCallListen builder.setOngoing(true); // Make the notification prioritized over the other normal notifications. builder.setPriority(Notification.PRIORITY_HIGH); - builder.setGroup(NOTIFICATION_TAG); + builder.setGroup(NOTIFICATION_GROUP); boolean isVideoCall = VideoProfile.isVideo(info.getCall().getDetails().getVideoState()); // Set the content ("Ongoing call on another device") @@ -249,6 +252,9 @@ public class ExternalCallNotifier implements ExternalCallList.ExternalCallListen builder.setColor(mContext.getResources().getColor(R.color.dialer_theme_color)); builder.addPerson(info.getPersonReference()); + NotificationChannelManager.applyChannel( + builder, mContext, Channel.EXTERNAL_CALL, info.getCall().getDetails().getAccountHandle()); + // Where the external call supports being transferred to the local device, add an action // to the notification to initiate the call pull process. if (CallCompat.canPullExternalCall(info.getCall())) { @@ -281,12 +287,19 @@ public class ExternalCallNotifier implements ExternalCallList.ExternalCallListen publicBuilder.setSmallIcon(R.drawable.quantum_ic_call_white_24); publicBuilder.setColor(mContext.getResources().getColor(R.color.dialer_theme_color)); + NotificationChannelManager.applyChannel( + publicBuilder, + mContext, + Channel.EXTERNAL_CALL, + info.getCall().getDetails().getAccountHandle()); + builder.setPublicVersion(publicBuilder.build()); Notification notification = builder.build(); NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); - notificationManager.notify(NOTIFICATION_TAG, info.getNotificationId(), notification); + notificationManager.notify( + String.valueOf(info.getNotificationId()), NOTIFICATION_ID, notification); if (!mShowingSummary && mNotifications.size() > 1) { // If the number of notifications shown is > 1, and we're not already showing a group summary, @@ -297,10 +310,12 @@ public class ExternalCallNotifier implements ExternalCallList.ExternalCallListen summary.setOngoing(true); // Make the notification prioritized over the other normal notifications. summary.setPriority(Notification.PRIORITY_HIGH); - summary.setGroup(NOTIFICATION_TAG); + summary.setGroup(NOTIFICATION_GROUP); summary.setGroupSummary(true); summary.setSmallIcon(R.drawable.quantum_ic_call_white_24); - notificationManager.notify(NOTIFICATION_TAG, SUMMARY_ID, summary.build()); + NotificationChannelManager.applyChannel( + summary, mContext, Channel.EXTERNAL_CALL, info.getCall().getDetails().getAccountHandle()); + notificationManager.notify(NOTIFICATION_GROUP, NOTIFICATION_ID, summary.build()); mShowingSummary = true; } } diff --git a/java/com/android/incallui/InCallActivity.java b/java/com/android/incallui/InCallActivity.java index 307415916..7c4394872 100644 --- a/java/com/android/incallui/InCallActivity.java +++ b/java/com/android/incallui/InCallActivity.java @@ -32,6 +32,7 @@ import android.view.KeyEvent; import android.view.MenuItem; import android.view.MotionEvent; import android.view.View; +import com.android.dialer.common.Assert; import com.android.dialer.common.LogUtil; import com.android.dialer.compat.ActivityCompat; import com.android.dialer.logging.Logger; @@ -44,7 +45,6 @@ import com.android.incallui.answerproximitysensor.PseudoScreenState; import com.android.incallui.call.CallList; import com.android.incallui.call.DialerCall; import com.android.incallui.call.DialerCall.State; -import com.android.incallui.call.VideoUtils; import com.android.incallui.incall.bindings.InCallBindings; import com.android.incallui.incall.protocol.InCallButtonUiDelegate; import com.android.incallui.incall.protocol.InCallButtonUiDelegateFactory; @@ -89,11 +89,7 @@ public class InCallActivity extends TransactionSafeFragmentActivity } public static Intent getIntent( - Context context, - boolean showDialpad, - boolean newOutgoingCall, - boolean isVideoCall, - boolean isForFullScreen) { + Context context, boolean showDialpad, boolean newOutgoingCall, boolean isForFullScreen) { Intent intent = new Intent(Intent.ACTION_MAIN, null); intent.setFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION | Intent.FLAG_ACTIVITY_NEW_TASK); intent.setClass(context, InCallActivity.class); @@ -192,7 +188,22 @@ public class InCallActivity extends TransactionSafeFragmentActivity @Override public void finish() { if (shouldCloseActivityOnFinish()) { - super.finish(); + // When user select incall ui from recents after the call is disconnected, it tries to launch + // a new InCallActivity but InCallPresenter is already teared down at this point, which causes + // crash. + // By calling finishAndRemoveTask() instead of finish() the task associated with + // InCallActivity is cleared completely. So system won't try to create a new InCallActivity in + // this case. + // + // Calling finish won't clear the task and normally when an activity finishes it shouldn't + // clear the task since there could be parent activity in the same task that's still alive. + // But InCallActivity is special since it's singleInstance which means it's root activity and + // only instance of activity in the task. So it should be safe to also remove task when + // finishing. + // It's also necessary in the sense of it's excluded from recents. So whenever the activity + // finishes, the task should also be removed since it doesn't make sense to go back to it in + // anyway anymore. + super.finishAndRemoveTask(); } } @@ -260,18 +271,12 @@ public class InCallActivity extends TransactionSafeFragmentActivity @Override public boolean onKeyUp(int keyCode, KeyEvent event) { - if (common.onKeyUp(keyCode, event)) { - return true; - } - return super.onKeyUp(keyCode, event); + return common.onKeyUp(keyCode, event) || super.onKeyUp(keyCode, event); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { - if (common.onKeyDown(keyCode, event)) { - return true; - } - return super.onKeyDown(keyCode, event); + return common.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event); } public boolean isInCallScreenAnimating() { @@ -411,13 +416,6 @@ public class InCallActivity extends TransactionSafeFragmentActivity common.setExcludeFromRecents(exclude); } - public void onResolveIntent( - DialerCall outgoingCall, boolean isNewOutgoingCall, boolean didShowAccountSelectionDialog) { - if (didShowAccountSelectionDialog) { - hideMainInCallFragment(); - } - } - @Nullable public FragmentManager getDialpadFragmentManager() { InCallScreen inCallScreen = getInCallScreen(); @@ -488,7 +486,7 @@ public class InCallActivity extends TransactionSafeFragmentActivity enableInCallOrientationEventListener(allowOrientationChange); } - private void hideMainInCallFragment() { + public void hideMainInCallFragment() { LogUtil.i("InCallActivity.hideMainInCallFragment", ""); if (didShowInCallScreen || didShowVideoCallScreen) { FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); @@ -513,8 +511,8 @@ public class InCallActivity extends TransactionSafeFragmentActivity } isInShowMainInCallFragment = true; - ShouldShowAnswerUiResult shouldShowAnswerUi = getShouldShowAnswerUi(); - boolean shouldShowVideoUi = getShouldShowVideoUi(); + ShouldShowUiResult shouldShowAnswerUi = getShouldShowAnswerUi(); + ShouldShowUiResult shouldShowVideoUi = getShouldShowVideoUi(); LogUtil.i( "InCallActivity.showMainInCallFragment", "shouldShowAnswerUi: %b, shouldShowVideoUi: %b, " @@ -525,7 +523,7 @@ public class InCallActivity extends TransactionSafeFragmentActivity didShowInCallScreen, didShowVideoCallScreen); // Only video call ui allows orientation change. - setAllowOrientationChange(shouldShowVideoUi); + setAllowOrientationChange(shouldShowVideoUi.shouldShow); FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); boolean didChangeInCall; @@ -535,9 +533,9 @@ public class InCallActivity extends TransactionSafeFragmentActivity didChangeInCall = hideInCallScreenFragment(transaction); didChangeVideo = hideVideoCallScreenFragment(transaction); didChangeAnswer = showAnswerScreenFragment(transaction, shouldShowAnswerUi.call); - } else if (shouldShowVideoUi) { + } else if (shouldShowVideoUi.shouldShow) { didChangeInCall = hideInCallScreenFragment(transaction); - didChangeVideo = showVideoCallScreenFragment(transaction); + didChangeVideo = showVideoCallScreenFragment(transaction, shouldShowVideoUi.call); didChangeAnswer = hideAnswerScreenFragment(transaction); } else { didChangeInCall = showInCallScreenFragment(transaction); @@ -552,17 +550,17 @@ public class InCallActivity extends TransactionSafeFragmentActivity isInShowMainInCallFragment = false; } - private ShouldShowAnswerUiResult getShouldShowAnswerUi() { + private ShouldShowUiResult getShouldShowAnswerUi() { DialerCall call = CallList.getInstance().getIncomingCall(); if (call != null) { LogUtil.i("InCallActivity.getShouldShowAnswerUi", "found incoming call"); - return new ShouldShowAnswerUiResult(true, call); + return new ShouldShowUiResult(true, call); } call = CallList.getInstance().getVideoUpgradeRequestCall(); if (call != null) { LogUtil.i("InCallActivity.getShouldShowAnswerUi", "found video upgrade request"); - return new ShouldShowAnswerUiResult(true, call); + return new ShouldShowUiResult(true, call); } // Check if we're showing the answer screen and the call is disconnected. If this condition is @@ -574,30 +572,30 @@ public class InCallActivity extends TransactionSafeFragmentActivity } if (didShowAnswerScreen && (call == null || call.getState() == State.DISCONNECTED)) { LogUtil.i("InCallActivity.getShouldShowAnswerUi", "found disconnecting incoming call"); - return new ShouldShowAnswerUiResult(true, call); + return new ShouldShowUiResult(true, call); } - return new ShouldShowAnswerUiResult(false, null); + return new ShouldShowUiResult(false, null); } - private boolean getShouldShowVideoUi() { + private static ShouldShowUiResult getShouldShowVideoUi() { DialerCall call = CallList.getInstance().getFirstCall(); if (call == null) { LogUtil.i("InCallActivity.getShouldShowVideoUi", "null call"); - return false; + return new ShouldShowUiResult(false, null); } - if (VideoUtils.isVideoCall(call)) { + if (call.isVideoCall()) { LogUtil.i("InCallActivity.getShouldShowVideoUi", "found video call"); - return true; + return new ShouldShowUiResult(true, call); } - if (VideoUtils.hasSentVideoUpgradeRequest(call)) { + if (call.hasSentVideoUpgradeRequest()) { LogUtil.i("InCallActivity.getShouldShowVideoUi", "upgrading to video"); - return true; + return new ShouldShowUiResult(true, call); } - return false; + return new ShouldShowUiResult(false, null); } private boolean showAnswerScreenFragment(FragmentTransaction transaction, DialerCall call) { @@ -607,14 +605,15 @@ public class InCallActivity extends TransactionSafeFragmentActivity return false; } - boolean isVideoUpgradeRequest = VideoUtils.hasReceivedVideoUpgradeRequest(call); - int videoState = isVideoUpgradeRequest ? call.getRequestedVideoState() : call.getVideoState(); + Assert.checkArgument(call != null, "didShowAnswerScreen was false but call was still null"); + + boolean isVideoUpgradeRequest = call.hasReceivedVideoUpgradeRequest(); // Check if we're already showing an answer screen for this call. if (didShowAnswerScreen) { AnswerScreen answerScreen = getAnswerScreen(); if (answerScreen.getCallId().equals(call.getId()) - && answerScreen.getVideoState() == videoState + && answerScreen.isVideoCall() == call.isVideoCall() && answerScreen.isVideoUpgradeRequest() == isVideoUpgradeRequest) { return false; } @@ -626,7 +625,7 @@ public class InCallActivity extends TransactionSafeFragmentActivity // Show a new answer screen. AnswerScreen answerScreen = - AnswerBindings.createAnswerScreen(call.getId(), videoState, isVideoUpgradeRequest); + AnswerBindings.createAnswerScreen(call.getId(), call.isVideoCall(), isVideoUpgradeRequest); transaction.add(R.id.main, answerScreen.getAnswerScreenFragment(), TAG_ANSWER_SCREEN); Logger.get(this).logScreenView(ScreenEvent.Type.INCOMING_CALL, this); @@ -675,12 +674,21 @@ public class InCallActivity extends TransactionSafeFragmentActivity return true; } - private boolean showVideoCallScreenFragment(FragmentTransaction transaction) { + private boolean showVideoCallScreenFragment(FragmentTransaction transaction, DialerCall call) { if (didShowVideoCallScreen) { - return false; + VideoCallScreen videoCallScreen = getVideoCallScreen(); + if (videoCallScreen.getCallId().equals(call.getId())) { + return false; + } + LogUtil.i( + "InCallActivity.showVideoCallScreenFragment", + "video call fragment exists but arguments do not match"); + hideVideoCallScreenFragment(transaction); } - VideoCallScreen videoCallScreen = VideoBindings.createVideoCallScreen(); + LogUtil.i("InCallActivity.showVideoCallScreenFragment", "call: %s", call); + + VideoCallScreen videoCallScreen = VideoBindings.createVideoCallScreen(call.getId()); transaction.add(R.id.main, videoCallScreen.getVideoCallScreenFragment(), TAG_VIDEO_CALL_SCREEN); Logger.get(this).logScreenView(ScreenEvent.Type.INCALL, this); @@ -744,11 +752,11 @@ public class InCallActivity extends TransactionSafeFragmentActivity return super.dispatchTouchEvent(event); } - private static class ShouldShowAnswerUiResult { + private static class ShouldShowUiResult { public final boolean shouldShow; public final DialerCall call; - ShouldShowAnswerUiResult(boolean shouldShow, DialerCall call) { + ShouldShowUiResult(boolean shouldShow, DialerCall call) { this.shouldShow = shouldShow; this.call = call; } diff --git a/java/com/android/incallui/InCallActivityCommon.java b/java/com/android/incallui/InCallActivityCommon.java index a2467dd72..2cdb913ce 100644 --- a/java/com/android/incallui/InCallActivityCommon.java +++ b/java/com/android/incallui/InCallActivityCommon.java @@ -21,7 +21,6 @@ import android.app.ActivityManager.AppTask; import android.app.ActivityManager.TaskDescription; import android.app.AlertDialog; import android.app.Dialog; -import android.app.DialogFragment; import android.content.Context; import android.content.DialogInterface; import android.content.DialogInterface.OnCancelListener; @@ -99,6 +98,7 @@ public class InCallActivityCommon { private String showPostCharWaitDialogCallId; private String showPostCharWaitDialogChars; private Dialog dialog; + private SelectPhoneAccountDialogFragment selectPhoneAccountDialogFragment; private InCallOrientationEventListener inCallOrientationEventListener; private Animation dialpadSlideInAnimation; private Animation dialpadSlideOutAnimation; @@ -496,11 +496,15 @@ public class InCallActivityCommon { } } - public void dismissPendingDialogs() { + void dismissPendingDialogs() { if (dialog != null) { dialog.dismiss(); dialog = null; } + if (selectPhoneAccountDialogFragment != null) { + selectPhoneAccountDialogFragment.dismiss(); + selectPhoneAccountDialogFragment = null; + } } private static boolean shouldShowDisconnectErrorDialog(@NonNull DisconnectCause cause) { @@ -769,9 +773,7 @@ public class InCallActivityCommon { outgoingCall = CallList.getInstance().getPendingOutgoingCall(); } - boolean isNewOutgoingCall = false; if (intent.getBooleanExtra(INTENT_EXTRA_NEW_OUTGOING_CALL, false)) { - isNewOutgoingCall = true; intent.removeExtra(INTENT_EXTRA_NEW_OUTGOING_CALL); // InCallActivity is responsible for disconnecting a new outgoing call if there @@ -789,16 +791,18 @@ public class InCallActivityCommon { } boolean didShowAccountSelectionDialog = maybeShowAccountSelectionDialog(); - inCallActivity.onResolveIntent(outgoingCall, isNewOutgoingCall, didShowAccountSelectionDialog); + if (didShowAccountSelectionDialog) { + inCallActivity.hideMainInCallFragment(); + } } private boolean maybeShowAccountSelectionDialog() { - DialerCall call = CallList.getInstance().getWaitingForAccountCall(); - if (call == null) { + DialerCall waitingForAccountCall = CallList.getInstance().getWaitingForAccountCall(); + if (waitingForAccountCall == null) { return false; } - Bundle extras = call.getIntentExtras(); + Bundle extras = waitingForAccountCall.getIntentExtras(); List phoneAccountHandles; if (extras != null) { phoneAccountHandles = @@ -807,14 +811,15 @@ public class InCallActivityCommon { phoneAccountHandles = new ArrayList<>(); } - DialogFragment dialogFragment = + selectPhoneAccountDialogFragment = SelectPhoneAccountDialogFragment.newInstance( R.string.select_phone_account_for_calls, true, phoneAccountHandles, selectAccountListener, - call.getId()); - dialogFragment.show(inCallActivity.getFragmentManager(), TAG_SELECT_ACCOUNT_FRAGMENT); + waitingForAccountCall.getId()); + selectPhoneAccountDialogFragment.show( + inCallActivity.getFragmentManager(), TAG_SELECT_ACCOUNT_FRAGMENT); return true; } } diff --git a/java/com/android/incallui/InCallPresenter.java b/java/com/android/incallui/InCallPresenter.java index 97105fb78..0f3982ce4 100644 --- a/java/com/android/incallui/InCallPresenter.java +++ b/java/com/android/incallui/InCallPresenter.java @@ -42,23 +42,22 @@ import com.android.dialer.blocking.FilteredNumbersUtil; import com.android.dialer.common.LogUtil; import com.android.dialer.logging.Logger; import com.android.dialer.logging.nano.InteractionEvent; +import com.android.dialer.postcall.PostCall; import com.android.dialer.telecom.TelecomUtil; import com.android.dialer.util.TouchPointManager; import com.android.incallui.InCallOrientationEventListener.ScreenOrientation; import com.android.incallui.answerproximitysensor.PseudoScreenState; import com.android.incallui.call.CallList; import com.android.incallui.call.DialerCall; -import com.android.incallui.call.DialerCall.SessionModificationState; import com.android.incallui.call.ExternalCallList; -import com.android.incallui.call.InCallVideoCallCallbackNotifier; import com.android.incallui.call.TelecomAdapter; -import com.android.incallui.call.VideoUtils; import com.android.incallui.latencyreport.LatencyReport; import com.android.incallui.legacyblocking.BlockedNumberContentObserver; import com.android.incallui.spam.SpamCallListListener; import com.android.incallui.util.TelecomCallUtil; import com.android.incallui.videosurface.bindings.VideoSurfaceBindings; import com.android.incallui.videosurface.protocol.VideoSurfaceTexture; +import com.android.incallui.videotech.VideoTech; import java.util.Collections; import java.util.List; import java.util.Objects; @@ -74,8 +73,7 @@ import java.util.concurrent.atomic.AtomicBoolean; * presenters that want to listen in on the in-call state changes. TODO: This class has become more * of a state machine at this point. Consider renaming. */ -public class InCallPresenter - implements CallList.Listener, InCallVideoCallCallbackNotifier.SessionModificationListener { +public class InCallPresenter implements CallList.Listener { private static final String EXTRA_FIRST_TIME_SHOWN = "com.android.incallui.intent.extra.FIRST_TIME_SHOWN"; @@ -173,7 +171,6 @@ public class InCallPresenter private ProximitySensor mProximitySensor; private final PseudoScreenState mPseudoScreenState = new PseudoScreenState(); private boolean mServiceConnected; - private boolean mAccountSelectionCancelled; private InCallCameraManager mInCallCameraManager; private FilteredNumberAsyncQueryHandler mFilteredQueryHandler; private CallList.Listener mSpamCallListListener; @@ -347,7 +344,6 @@ public class InCallPresenter mCallList.addListener(mSpamCallListListener); VideoPauseController.getInstance().setUp(this); - InCallVideoCallCallbackNotifier.getInstance().addSessionModificationListener(this); mFilteredQueryHandler = new FilteredNumberAsyncQueryHandler(context); mContext @@ -376,7 +372,6 @@ public class InCallPresenter attemptCleanup(); VideoPauseController.getInstance().tearDown(); - InCallVideoCallCallbackNotifier.getInstance().removeSessionModificationListener(this); } private void attemptFinishActivity() { @@ -385,12 +380,6 @@ public class InCallPresenter if (doFinish) { mInCallActivity.setExcludeFromRecents(true); mInCallActivity.finish(); - - if (mAccountSelectionCancelled) { - // This finish is a result of account selection cancellation - // do not include activity ending transition - mInCallActivity.overridePendingTransition(0, 0); - } } } @@ -664,6 +653,19 @@ public class InCallPresenter InCallState newState = getPotentialStateFromCallList(callList); InCallState oldState = mInCallState; Log.d(this, "onCallListChange oldState= " + oldState + " newState=" + newState); + + // If the user placed a call and was asked to choose the account, but then pressed "Home", the + // incall activity for that call will still exist (even if it's not visible). In the case of + // an incoming call in that situation, just disconnect that "waiting for account" call and + // dismiss the dialog. The same activity will be reused to handle the new incoming call. See + // b/33247755 for more details. + DialerCall waitingForAccountCall; + if (newState == InCallState.INCOMING + && (waitingForAccountCall = callList.getWaitingForAccountCall()) != null) { + waitingForAccountCall.disconnect(); + mInCallActivity.dismissPendingDialogs(); + } + newState = startOrFinishUi(newState); Log.d(this, "onCallListChange newState changed to " + newState); @@ -705,13 +707,13 @@ public class InCallPresenter @Override public void onUpgradeToVideo(DialerCall call) { - if (call.getSessionModificationState() - == DialerCall.SESSION_MODIFICATION_STATE_RECEIVED_UPGRADE_TO_VIDEO_REQUEST + if (call.getVideoTech().getSessionModificationState() + == VideoTech.SESSION_MODIFICATION_STATE_RECEIVED_UPGRADE_TO_VIDEO_REQUEST && mInCallState == InCallPresenter.InCallState.INCOMING) { LogUtil.i( "InCallPresenter.onUpgradeToVideo", "rejecting upgrade request due to existing incoming call"); - call.declineUpgradeRequest(); + call.getVideoTech().declineVideoRequest(); } if (mInCallActivity != null) { @@ -721,15 +723,15 @@ public class InCallPresenter } @Override - public void onSessionModificationStateChange(@SessionModificationState int newState) { + public void onSessionModificationStateChange(DialerCall call) { + int newState = call.getVideoTech().getSessionModificationState(); LogUtil.i("InCallPresenter.onSessionModificationStateChange", "state: %d", newState); if (mProximitySensor == null) { LogUtil.i("InCallPresenter.onSessionModificationStateChange", "proximitySensor is null"); return; } mProximitySensor.setIsAttemptingVideoCall( - VideoUtils.hasSentVideoUpgradeRequest(newState) - || VideoUtils.hasReceivedVideoUpgradeRequest(newState)); + call.hasSentVideoUpgradeRequest() || call.hasReceivedVideoUpgradeRequest()); if (mInCallActivity != null) { // Re-evaluate which fragment is being shown. mInCallActivity.onPrimaryCallStateChanged(); @@ -754,19 +756,10 @@ public class InCallPresenter if (call.isEmergencyCall()) { FilteredNumbersUtil.recordLastEmergencyCallTime(mContext); } - } - - @Override - public void onUpgradeToVideoRequest(DialerCall call, int videoState) { - LogUtil.d( - "InCallPresenter.onUpgradeToVideoRequest", - "call = " + call + " video state = " + videoState); - if (call == null) { - return; + if (!call.getLogState().isIncoming && !mCallList.hasLiveCall()) { + PostCall.onCallDisconnected(mContext, call.getNumber(), call.getConnectTimeMillis()); } - - call.setRequestedVideoState(videoState); } /** Given the call list, return the state in which the in-call screen should be. */ @@ -916,6 +909,24 @@ public class InCallPresenter && !mInCallActivity.isFinishing()); } + private boolean isActivityVisible() { + return mInCallActivity != null && mInCallActivity.isVisible(); + } + + boolean shouldShowFullScreenNotification() { + /** + * This is to cover the case where the incall activity is started but in the background, e.g. + * when the user pressed Home from the account selection dialog or an existing call. In the case + * that incall activity is already visible, there's no need to configure the notification with a + * full screen intent. + */ + LogUtil.d( + "InCallPresenter.shouldShowFullScreenNotification", + "isActivityVisible: %b", + isActivityVisible()); + return !isActivityVisible(); + } + /** * Determines if the In-Call app is currently changing configuration. * @@ -1018,7 +1029,7 @@ public class InCallPresenter // present (e.g. a call was accepted by a bluetooth or wired headset), we want to // bring it up the UI regardless. if (!isShowingInCallUi() && mInCallState != InCallState.NO_CALLS) { - showInCall(showDialpad, false /* newOutgoingCall */, false /* isVideoCall */); + showInCall(showDialpad, false /* newOutgoingCall */); } } @@ -1281,7 +1292,7 @@ public class InCallPresenter if (showCallUi || showAccountPicker) { Log.i(this, "Start in call UI"); - showInCall(false /* showDialpad */, !showAccountPicker /* newOutgoingCall */, false); + showInCall(false /* showDialpad */, !showAccountPicker /* newOutgoingCall */); } else if (startIncomingCallSequence) { Log.i(this, "Start Full Screen in call UI"); @@ -1332,7 +1343,7 @@ public class InCallPresenter mCallList.getActiveCall() != null && mCallList.getIncomingCall() != null; if (isCallWaiting) { - showInCall(false, false, false /* isVideoCall */); + showInCall(false, false); } else { mStatusBarNotifier.updateNotification(mCallList); } @@ -1403,11 +1414,11 @@ public class InCallPresenter } } - public void showInCall(boolean showDialpad, boolean newOutgoingCall, boolean isVideoCall) { + public void showInCall(boolean showDialpad, boolean newOutgoingCall) { Log.i(this, "Showing InCallActivity"); mContext.startActivity( InCallActivity.getIntent( - mContext, showDialpad, newOutgoingCall, isVideoCall, false /* forFullScreen */)); + mContext, showDialpad, newOutgoingCall, false /* forFullScreen */)); } public void onServiceBind() { @@ -1441,15 +1452,11 @@ public class InCallPresenter final PhoneAccountHandle accountHandle = intent.getParcelableExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE); final Point touchPoint = extras.getParcelable(TouchPointManager.TOUCH_POINT); - int videoState = - extras.getInt( - TelecomManager.EXTRA_START_CALL_WITH_VIDEO_STATE, VideoProfile.STATE_AUDIO_ONLY); InCallPresenter.getInstance().setBoundAndWaitingForOutgoingCall(true, accountHandle); final Intent activityIntent = - InCallActivity.getIntent( - mContext, false, true, VideoUtils.isVideoCall(videoState), false /* forFullScreen */); + InCallActivity.getIntent(mContext, false, true, false /* forFullScreen */); activityIntent.putExtra(TouchPointManager.TOUCH_POINT, touchPoint); mContext.startActivity(activityIntent); } diff --git a/java/com/android/incallui/NotificationBroadcastReceiver.java b/java/com/android/incallui/NotificationBroadcastReceiver.java index 5c5d255cc..cef18958e 100644 --- a/java/com/android/incallui/NotificationBroadcastReceiver.java +++ b/java/com/android/incallui/NotificationBroadcastReceiver.java @@ -27,7 +27,6 @@ import com.android.dialer.logging.Logger; import com.android.dialer.logging.nano.DialerImpression; import com.android.incallui.call.CallList; import com.android.incallui.call.DialerCall; -import com.android.incallui.call.VideoUtils; /** * Accepts broadcast Intents which will be prepared by {@link StatusBarNotifier} and thus sent from @@ -96,7 +95,7 @@ public class NotificationBroadcastReceiver extends BroadcastReceiver { } else { DialerCall call = callList.getVideoUpgradeRequestCall(); if (call != null) { - call.acceptUpgradeRequest(call.getRequestedVideoState()); + call.getVideoTech().acceptVideoRequest(); } } } @@ -109,7 +108,7 @@ public class NotificationBroadcastReceiver extends BroadcastReceiver { } else { DialerCall call = callList.getVideoUpgradeRequestCall(); if (call != null) { - call.declineUpgradeRequest(); + call.getVideoTech().declineVideoRequest(); } } } @@ -142,10 +141,7 @@ public class NotificationBroadcastReceiver extends BroadcastReceiver { if (call != null) { call.answer(videoState); InCallPresenter.getInstance() - .showInCall( - false /* showDialpad */, - false /* newOutgoingCall */, - VideoUtils.isVideoCall(videoState)); + .showInCall(false /* showDialpad */, false /* newOutgoingCall */); } } } diff --git a/java/com/android/incallui/ProximitySensor.java b/java/com/android/incallui/ProximitySensor.java index 91220627c..229b58ce7 100644 --- a/java/com/android/incallui/ProximitySensor.java +++ b/java/com/android/incallui/ProximitySensor.java @@ -28,7 +28,7 @@ import com.android.incallui.AudioModeProvider.AudioModeListener; import com.android.incallui.InCallPresenter.InCallState; import com.android.incallui.InCallPresenter.InCallStateListener; import com.android.incallui.call.CallList; -import com.android.incallui.call.VideoUtils; +import com.android.incallui.call.DialerCall; /** * Class manages the proximity sensor for the in-call UI. We enable the proximity sensor while the @@ -103,7 +103,8 @@ public class ProximitySensor boolean hasOngoingCall = InCallState.INCALL == newState && callList.hasLiveCall(); boolean isOffhook = (InCallState.OUTGOING == newState) || hasOngoingCall; - boolean isVideoCall = VideoUtils.isVideoCall(callList.getActiveCall()); + DialerCall activeCall = callList.getActiveCall(); + boolean isVideoCall = activeCall != null && activeCall.isVideoCall(); if (isOffhook != mIsPhoneOffhook || mIsVideoCall != isVideoCall) { mIsPhoneOffhook = isOffhook; diff --git a/java/com/android/incallui/StatusBarNotifier.java b/java/com/android/incallui/StatusBarNotifier.java index c7226753f..d6262be18 100644 --- a/java/com/android/incallui/StatusBarNotifier.java +++ b/java/com/android/incallui/StatusBarNotifier.java @@ -24,8 +24,10 @@ import static com.android.incallui.NotificationBroadcastReceiver.ACTION_DECLINE_ import static com.android.incallui.NotificationBroadcastReceiver.ACTION_DECLINE_VIDEO_UPGRADE_REQUEST; import static com.android.incallui.NotificationBroadcastReceiver.ACTION_HANG_UP_ONGOING_CALL; +import android.Manifest; import android.app.ActivityManager; import android.app.Notification; +import android.app.Notification.Builder; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; @@ -34,6 +36,7 @@ import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; +import android.graphics.drawable.Icon; import android.media.AudioAttributes; import android.net.Uri; import android.os.Build.VERSION; @@ -41,10 +44,13 @@ import android.os.Build.VERSION_CODES; import android.support.annotation.ColorRes; import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.support.annotation.RequiresPermission; import android.support.annotation.StringRes; import android.support.annotation.VisibleForTesting; +import android.support.v4.os.BuildCompat; import android.telecom.Call.Details; import android.telecom.PhoneAccount; +import android.telecom.PhoneAccountHandle; import android.telecom.TelecomManager; import android.text.BidiFormatter; import android.text.Spannable; @@ -54,10 +60,13 @@ import android.text.TextUtils; import android.text.style.ForegroundColorSpan; import com.android.contacts.common.ContactsUtils; import com.android.contacts.common.ContactsUtils.UserType; +import com.android.contacts.common.lettertiles.LetterTileDrawable; import com.android.contacts.common.preference.ContactsPreferences; import com.android.contacts.common.util.BitmapUtil; import com.android.contacts.common.util.ContactDisplayUtils; import com.android.dialer.common.LogUtil; +import com.android.dialer.notification.NotificationChannelManager; +import com.android.dialer.notification.NotificationChannelManager.Channel; import com.android.dialer.util.DrawableConverter; import com.android.incallui.ContactInfoCache.ContactCacheEntry; import com.android.incallui.ContactInfoCache.ContactInfoCacheCallback; @@ -65,11 +74,13 @@ import com.android.incallui.InCallPresenter.InCallState; import com.android.incallui.async.PausableExecutorImpl; import com.android.incallui.call.CallList; import com.android.incallui.call.DialerCall; -import com.android.incallui.call.DialerCall.SessionModificationState; import com.android.incallui.call.DialerCallListener; import com.android.incallui.ringtone.DialerRingtoneManager; import com.android.incallui.ringtone.InCallTonePlayer; import com.android.incallui.ringtone.ToneGeneratorFactory; +import com.android.incallui.videotech.VideoTech; +import java.util.List; +import java.util.Locale; import java.util.Objects; /** This class adds Notifications to the status bar for the in-call experience. */ @@ -79,9 +90,9 @@ public class StatusBarNotifier implements InCallPresenter.InCallStateListener { // Indicates that no notification is currently showing. private static final int NOTIFICATION_NONE = 0; // Notification for an active call. This is non-interruptive, but cannot be dismissed. - private static final int NOTIFICATION_IN_CALL = 1; + private static final int NOTIFICATION_IN_CALL = R.id.notification_ongoing_call; // Notification for incoming calls. This is interruptive and will show up as a HUN. - private static final int NOTIFICATION_INCOMING_CALL = 2; + private static final int NOTIFICATION_INCOMING_CALL = R.id.notification_incoming_call; private static final int PENDING_INTENT_REQUEST_CODE_NON_FULL_SCREEN = 0; private static final int PENDING_INTENT_REQUEST_CODE_FULL_SCREEN = 1; @@ -101,8 +112,9 @@ public class StatusBarNotifier implements InCallPresenter.InCallStateListener { private String mSavedContentTitle; private Uri mRingtone; private StatusBarCallListener mStatusBarCallListener; + private boolean mShowFullScreenIntent; - public StatusBarNotifier(@NonNull Context context, @NonNull ContactInfoCache contactInfoCache) { + StatusBarNotifier(@NonNull Context context, @NonNull ContactInfoCache contactInfoCache) { Objects.requireNonNull(context); mContext = context; mContactsPreferences = ContactsPreferencesFactory.newContactsPreferences(mContext); @@ -120,9 +132,9 @@ public class StatusBarNotifier implements InCallPresenter.InCallStateListener { * notifications. */ static void clearAllCallNotifications(Context backupContext) { - Log.i( - StatusBarNotifier.class.getSimpleName(), - "Something terrible happened. Clear all InCall notifications"); + LogUtil.i( + "StatusBarNotifier.clearAllCallNotifications", + "something terrible happened, clear all InCall notifications"); NotificationManager notificationManager = backupContext.getSystemService(NotificationManager.class); @@ -153,10 +165,17 @@ public class StatusBarNotifier implements InCallPresenter.InCallStateListener { return PendingIntent.getBroadcast(context, 0, intent, 0); } + private static void setColorized(@NonNull Builder builder) { + if (BuildCompat.isAtLeastO()) { + builder.setColorized(true); + } + } + /** Creates notifications according to the state we receive from {@link InCallPresenter}. */ @Override + @RequiresPermission(Manifest.permission.READ_PHONE_STATE) public void onStateChange(InCallState oldState, InCallState newState, CallList callList) { - Log.d(this, "onStateChange"); + LogUtil.d("StatusBarNotifier.onStateChange", "%s->%s", oldState, newState); updateNotification(callList); } @@ -177,7 +196,8 @@ public class StatusBarNotifier implements InCallPresenter.InCallStateListener { * * @see #updateInCallNotification(CallList) */ - public void updateNotification(CallList callList) { + @RequiresPermission(Manifest.permission.READ_PHONE_STATE) + void updateNotification(CallList callList) { updateInCallNotification(callList); } @@ -191,7 +211,7 @@ public class StatusBarNotifier implements InCallPresenter.InCallStateListener { setStatusBarCallListener(null); } if (mCurrentNotification != NOTIFICATION_NONE) { - Log.d(this, "cancelInCall()..."); + LogUtil.d("StatusBarNotifier.cancelNotification", "cancel"); mNotificationManager.cancel(mCurrentNotification); } mCurrentNotification = NOTIFICATION_NONE; @@ -202,8 +222,9 @@ public class StatusBarNotifier implements InCallPresenter.InCallStateListener { * status bar notification based on the current telephony state, or cancels the notification if * the phone is totally idle. */ + @RequiresPermission(Manifest.permission.READ_PHONE_STATE) private void updateInCallNotification(CallList callList) { - Log.d(this, "updateInCallNotification..."); + LogUtil.d("StatusBarNotifier.updateInCallNotification", ""); final DialerCall call = getCallToShow(callList); @@ -214,6 +235,7 @@ public class StatusBarNotifier implements InCallPresenter.InCallStateListener { } } + @RequiresPermission(Manifest.permission.READ_PHONE_STATE) private void showNotification(final CallList callList, final DialerCall call) { final boolean isIncoming = (call.getState() == DialerCall.State.INCOMING @@ -230,6 +252,7 @@ public class StatusBarNotifier implements InCallPresenter.InCallStateListener { isIncoming, new ContactInfoCacheCallback() { @Override + @RequiresPermission(Manifest.permission.READ_PHONE_STATE) public void onContactInfoComplete(String callId, ContactCacheEntry entry) { DialerCall call = callList.getCallById(callId); if (call != null) { @@ -239,6 +262,7 @@ public class StatusBarNotifier implements InCallPresenter.InCallStateListener { } @Override + @RequiresPermission(Manifest.permission.READ_PHONE_STATE) public void onImageLoadComplete(String callId, ContactCacheEntry entry) { DialerCall call = callList.getCallById(callId); if (call != null) { @@ -249,6 +273,7 @@ public class StatusBarNotifier implements InCallPresenter.InCallStateListener { } /** Sets up the main Ui for the notification */ + @RequiresPermission(Manifest.permission.READ_PHONE_STATE) private void buildAndSendNotification( CallList callList, DialerCall originalCall, ContactCacheEntry contactInfo) { // This can get called to update an existing notification after contact information has come @@ -268,8 +293,8 @@ public class StatusBarNotifier implements InCallPresenter.InCallStateListener { final String contentTitle = getContentTitle(contactInfo, call); final boolean isVideoUpgradeRequest = - call.getSessionModificationState() - == DialerCall.SESSION_MODIFICATION_STATE_RECEIVED_UPGRADE_TO_VIDEO_REQUEST; + call.getVideoTech().getSessionModificationState() + == VideoTech.SESSION_MODIFICATION_STATE_RECEIVED_UPGRADE_TO_VIDEO_REQUEST; final int notificationType; if (callState == DialerCall.State.INCOMING || callState == DialerCall.State.CALL_WAITING @@ -286,7 +311,8 @@ public class StatusBarNotifier implements InCallPresenter.InCallStateListener { contentTitle, callState, notificationType, - contactInfo.contactRingtoneUri)) { + contactInfo.contactRingtoneUri, + InCallPresenter.getInstance().shouldShowFullScreenNotification())) { return; } @@ -300,9 +326,10 @@ public class StatusBarNotifier implements InCallPresenter.InCallStateListener { Notification.Builder publicBuilder = new Notification.Builder(mContext); publicBuilder .setSmallIcon(iconResId) - .setColor(mContext.getResources().getColor(R.color.dialer_theme_color)) + .setColor(mContext.getResources().getColor(R.color.dialer_theme_color, mContext.getTheme())) // Hide work call state for the lock screen notification .setContentTitle(getContentString(call, ContactsUtils.USER_TYPE_CURRENT)); + setColorized(publicBuilder); setNotificationWhen(call, callState, publicBuilder); // Builder for the notification shown when the device is unlocked or the user has set their @@ -311,28 +338,26 @@ public class StatusBarNotifier implements InCallPresenter.InCallStateListener { builder.setPublicVersion(publicBuilder.build()); // Set up the main intent to send the user to the in-call screen - builder.setContentIntent( - createLaunchPendingIntent(false /* isFullScreen */, call.isVideoCall())); + builder.setContentIntent(createLaunchPendingIntent(false /* isFullScreen */)); // Set the intent as a full screen intent as well if a call is incoming + PhoneAccountHandle accountHandle = call.getAccountHandle(); + if (accountHandle == null) { + accountHandle = getAnyPhoneAccount(); + } if (notificationType == NOTIFICATION_INCOMING_CALL) { - if (!InCallPresenter.getInstance().isActivityStarted()) { + NotificationChannelManager.applyChannel( + builder, mContext, Channel.INCOMING_CALL, accountHandle); + if (InCallPresenter.getInstance().shouldShowFullScreenNotification()) { configureFullScreenIntent( - builder, - createLaunchPendingIntent(true /* isFullScreen */, call.isVideoCall()), - callList, - call); - } else { - // If the incall screen is already up, we don't want to show HUN but regular notification - // should still be shown. In order to do that the previous one with full screen intent - // needs to be cancelled. - LogUtil.d( - "StatusBarNotifier.buildAndSendNotification", - "cancel previous incoming call notification"); - mNotificationManager.cancel(NOTIFICATION_INCOMING_CALL); + builder, createLaunchPendingIntent(true /* isFullScreen */), callList, call); } - // Set the notification category for incoming calls + // Set the notification category and bump the priority for incoming calls builder.setCategory(Notification.CATEGORY_CALL); + builder.setPriority(Notification.PRIORITY_MAX); + } else { + NotificationChannelManager.applyChannel( + builder, mContext, Channel.ONGOING_CALL, accountHandle); } // Set the content @@ -340,7 +365,9 @@ public class StatusBarNotifier implements InCallPresenter.InCallStateListener { builder.setSmallIcon(iconResId); builder.setContentTitle(contentTitle); builder.setLargeIcon(largeIcon); - builder.setColor(mContext.getResources().getColor(R.color.dialer_theme_color)); + builder.setColor( + mContext.getResources().getColor(R.color.dialer_theme_color, mContext.getTheme())); + setColorized(builder); if (isVideoUpgradeRequest) { builder.setUsesChronometer(false); @@ -367,15 +394,20 @@ public class StatusBarNotifier implements InCallPresenter.InCallStateListener { } } if (mDialerRingtoneManager.shouldPlayCallWaitingTone(callState)) { - Log.v(this, "Playing call waiting tone"); + LogUtil.v("StatusBarNotifier.buildAndSendNotification", "playing call waiting tone"); mDialerRingtoneManager.playCallWaitingTone(); } if (mCurrentNotification != notificationType && mCurrentNotification != NOTIFICATION_NONE) { - Log.i(this, "Previous notification already showing - cancelling " + mCurrentNotification); + LogUtil.i( + "StatusBarNotifier.buildAndSendNotification", + "previous notification already showing - cancelling " + mCurrentNotification); mNotificationManager.cancel(mCurrentNotification); } - Log.i(this, "Displaying notification for " + notificationType); + LogUtil.i( + "StatusBarNotifier.buildAndSendNotification", + "displaying notification for " + notificationType); + try { mNotificationManager.notify(notificationType, notification); } catch (RuntimeException e) { @@ -385,14 +417,32 @@ public class StatusBarNotifier implements InCallPresenter.InCallStateListener { activityManager.getMemoryInfo(memoryInfo); throw new RuntimeException( String.format( + Locale.US, "Error displaying notification with photo type: %d (low memory? %b, availMem: %d)", - contactInfo.photoType, memoryInfo.lowMemory, memoryInfo.availMem), + contactInfo.photoType, + memoryInfo.lowMemory, + memoryInfo.availMem), e); } call.getLatencyReport().onNotificationShown(); mCurrentNotification = notificationType; } + @Nullable + @RequiresPermission(Manifest.permission.READ_PHONE_STATE) + private PhoneAccountHandle getAnyPhoneAccount() { + PhoneAccountHandle accountHandle; + TelecomManager telecomManager = mContext.getSystemService(TelecomManager.class); + accountHandle = telecomManager.getDefaultOutgoingPhoneAccount(PhoneAccount.SCHEME_TEL); + if (accountHandle == null) { + List accountHandles = telecomManager.getCallCapablePhoneAccounts(); + if (!accountHandles.isEmpty()) { + accountHandle = accountHandles.get(0); + } + } + return accountHandle; + } + private void createIncomingCallNotification( DialerCall call, int state, Notification.Builder builder) { setNotificationWhen(call, state, builder); @@ -438,7 +488,8 @@ public class StatusBarNotifier implements InCallPresenter.InCallStateListener { String contentTitle, int state, int notificationType, - Uri ringtone) { + Uri ringtone, + boolean showFullScreenIntent) { // The two are different: // if new title is not null, it should be different from saved version OR @@ -454,13 +505,15 @@ public class StatusBarNotifier implements InCallPresenter.InCallStateListener { || (mCallState != state) || (mSavedLargeIcon != largeIcon) || contentTitleChanged - || !Objects.equals(mRingtone, ringtone); + || !Objects.equals(mRingtone, ringtone) + || mShowFullScreenIntent != showFullScreenIntent; // If we aren't showing a notification right now or the notification type is changing, // definitely do an update. if (mCurrentNotification != notificationType) { if (mCurrentNotification == NOTIFICATION_NONE) { - Log.d(this, "Showing notification for first time."); + LogUtil.d( + "StatusBarNotifier.checkForChangeAndSaveData", "showing notification for first time."); } retval = true; } @@ -471,9 +524,11 @@ public class StatusBarNotifier implements InCallPresenter.InCallStateListener { mSavedLargeIcon = largeIcon; mSavedContentTitle = contentTitle; mRingtone = ringtone; + mShowFullScreenIntent = showFullScreenIntent; if (retval) { - Log.d(this, "Data changed. Showing notification"); + LogUtil.d( + "StatusBarNotifier.checkForChangeAndSaveData", "data changed. Showing notification"); } return retval; @@ -520,8 +575,34 @@ public class StatusBarNotifier implements InCallPresenter.InCallStateListener { if (contactInfo.photo != null && (contactInfo.photo instanceof BitmapDrawable)) { largeIcon = ((BitmapDrawable) contactInfo.photo).getBitmap(); } + if (contactInfo.photo == null) { + int width = + (int) mContext.getResources().getDimension(android.R.dimen.notification_large_icon_width); + int height = + (int) + mContext.getResources().getDimension(android.R.dimen.notification_large_icon_height); + int contactType = LetterTileDrawable.TYPE_DEFAULT; + LetterTileDrawable lettertile = new LetterTileDrawable(mContext.getResources()); + + // TODO: Deduplicate across Dialer. b/36195917 + if (CallerInfoUtils.isVoiceMailNumber(mContext, call)) { + contactType = LetterTileDrawable.TYPE_VOICEMAIL; + } else if (contactInfo.isBusiness) { + contactType = LetterTileDrawable.TYPE_BUSINESS; + } else if (call.getNumberPresentation() == TelecomManager.PRESENTATION_RESTRICTED) { + contactType = LetterTileDrawable.TYPE_GENERIC_AVATAR; + } + lettertile.setCanonicalDialerLetterTileDetails( + contactInfo.namePrimary == null ? contactInfo.number : contactInfo.namePrimary, + contactInfo.lookupKey, + LetterTileDrawable.SHAPE_CIRCLE, + contactType); + largeIcon = lettertile.getBitmap(width, height); + } + if (call.isSpam()) { - Drawable drawable = mContext.getResources().getDrawable(R.drawable.blocked_contact); + Drawable drawable = + mContext.getResources().getDrawable(R.drawable.blocked_contact, mContext.getTheme()); largeIcon = DrawableConverter.drawableToBitmap(drawable); } return largeIcon; @@ -552,8 +633,8 @@ public class StatusBarNotifier implements InCallPresenter.InCallStateListener { // display that regardless of the state of the other calls. if (call.getState() == DialerCall.State.ONHOLD) { return R.drawable.ic_phone_paused_white_24dp; - } else if (call.getSessionModificationState() - == DialerCall.SESSION_MODIFICATION_STATE_RECEIVED_UPGRADE_TO_VIDEO_REQUEST) { + } else if (call.getVideoTech().getSessionModificationState() + == VideoTech.SESSION_MODIFICATION_STATE_RECEIVED_UPGRADE_TO_VIDEO_REQUEST) { return R.drawable.ic_videocam; } return R.anim.on_going_call; @@ -594,8 +675,8 @@ public class StatusBarNotifier implements InCallPresenter.InCallStateListener { resId = R.string.notification_on_hold; } else if (DialerCall.State.isDialing(call.getState())) { resId = R.string.notification_dialing; - } else if (call.getSessionModificationState() - == DialerCall.SESSION_MODIFICATION_STATE_RECEIVED_UPGRADE_TO_VIDEO_REQUEST) { + } else if (call.getVideoTech().getSessionModificationState() + == VideoTech.SESSION_MODIFICATION_STATE_RECEIVED_UPGRADE_TO_VIDEO_REQUEST) { resId = R.string.notification_requesting_video_call; } @@ -639,64 +720,98 @@ public class StatusBarNotifier implements InCallPresenter.InCallStateListener { } private void addAnswerAction(Notification.Builder builder) { - Log.d(this, "Will show \"answer\" action in the incoming call Notification"); + LogUtil.d( + "StatusBarNotifier.addAnswerAction", + "will show \"answer\" action in the incoming call Notification"); PendingIntent answerVoicePendingIntent = createNotificationPendingIntent(mContext, ACTION_ANSWER_VOICE_INCOMING_CALL); + // We put animation resources in "anim" folder instead of "drawable", which causes Android + // Studio to complain. + // TODO: Move "anim" resources to "drawable" as recommended in AnimationDrawable doc? + //noinspection ResourceType builder.addAction( - R.anim.on_going_call, - getActionText(R.string.notification_action_answer, R.color.notification_action_accept), - answerVoicePendingIntent); + new Notification.Action.Builder( + Icon.createWithResource(mContext, R.anim.on_going_call), + getActionText( + R.string.notification_action_answer, R.color.notification_action_accept), + answerVoicePendingIntent) + .build()); } private void addDismissAction(Notification.Builder builder) { - Log.d(this, "Will show \"decline\" action in the incoming call Notification"); + LogUtil.d( + "StatusBarNotifier.addDismissAction", + "will show \"decline\" action in the incoming call Notification"); PendingIntent declinePendingIntent = createNotificationPendingIntent(mContext, ACTION_DECLINE_INCOMING_CALL); builder.addAction( - R.drawable.ic_close_dk, - getActionText(R.string.notification_action_dismiss, R.color.notification_action_dismiss), - declinePendingIntent); + new Notification.Action.Builder( + Icon.createWithResource(mContext, R.drawable.ic_close_dk), + getActionText( + R.string.notification_action_dismiss, R.color.notification_action_dismiss), + declinePendingIntent) + .build()); } private void addHangupAction(Notification.Builder builder) { - Log.d(this, "Will show \"hang-up\" action in the ongoing active call Notification"); + LogUtil.d( + "StatusBarNotifier.addHangupAction", + "will show \"hang-up\" action in the ongoing active call Notification"); PendingIntent hangupPendingIntent = createNotificationPendingIntent(mContext, ACTION_HANG_UP_ONGOING_CALL); builder.addAction( - R.drawable.ic_call_end_white_24dp, - getActionText(R.string.notification_action_end_call, R.color.notification_action_end_call), - hangupPendingIntent); + new Notification.Action.Builder( + Icon.createWithResource(mContext, R.drawable.ic_call_end_white_24dp), + getActionText( + R.string.notification_action_end_call, R.color.notification_action_end_call), + hangupPendingIntent) + .build()); } private void addVideoCallAction(Notification.Builder builder) { - Log.i(this, "Will show \"video\" action in the incoming call Notification"); + LogUtil.i( + "StatusBarNotifier.addVideoCallAction", + "will show \"video\" action in the incoming call Notification"); PendingIntent answerVideoPendingIntent = createNotificationPendingIntent(mContext, ACTION_ANSWER_VIDEO_INCOMING_CALL); builder.addAction( - R.drawable.ic_videocam, - getActionText( - R.string.notification_action_answer_video, R.color.notification_action_answer_video), - answerVideoPendingIntent); + new Notification.Action.Builder( + Icon.createWithResource(mContext, R.drawable.ic_videocam), + getActionText( + R.string.notification_action_answer_video, + R.color.notification_action_answer_video), + answerVideoPendingIntent) + .build()); } private void addAcceptUpgradeRequestAction(Notification.Builder builder) { - Log.i(this, "Will show \"accept upgrade\" action in the incoming call Notification"); + LogUtil.i( + "StatusBarNotifier.addAcceptUpgradeRequestAction", + "will show \"accept upgrade\" action in the incoming call Notification"); PendingIntent acceptVideoPendingIntent = createNotificationPendingIntent(mContext, ACTION_ACCEPT_VIDEO_UPGRADE_REQUEST); builder.addAction( - R.drawable.ic_videocam, - getActionText(R.string.notification_action_accept, R.color.notification_action_accept), - acceptVideoPendingIntent); + new Notification.Action.Builder( + Icon.createWithResource(mContext, R.drawable.ic_videocam), + getActionText( + R.string.notification_action_accept, R.color.notification_action_accept), + acceptVideoPendingIntent) + .build()); } private void addDismissUpgradeRequestAction(Notification.Builder builder) { - Log.i(this, "Will show \"dismiss upgrade\" action in the incoming call Notification"); + LogUtil.i( + "StatusBarNotifier.addDismissUpgradeRequestAction", + "will show \"dismiss upgrade\" action in the incoming call Notification"); PendingIntent declineVideoPendingIntent = createNotificationPendingIntent(mContext, ACTION_DECLINE_VIDEO_UPGRADE_REQUEST); builder.addAction( - R.drawable.ic_videocam, - getActionText(R.string.notification_action_dismiss, R.color.notification_action_dismiss), - declineVideoPendingIntent); + new Notification.Action.Builder( + Icon.createWithResource(mContext, R.drawable.ic_videocam), + getActionText( + R.string.notification_action_dismiss, R.color.notification_action_dismiss), + declineVideoPendingIntent) + .build()); } /** Adds fullscreen intent to the builder. */ @@ -707,7 +822,7 @@ public class StatusBarNotifier implements InCallPresenter.InCallStateListener { // to the status bar). Setting fullScreenIntent will cause // the InCallScreen to be launched immediately *unless* the // current foreground activity is marked as "immersive". - Log.d(this, "- Setting fullScreenIntent: " + intent); + LogUtil.d("StatusBarNotifier.configureFullScreenIntent", "setting fullScreenIntent: " + intent); builder.setFullScreenIntent(intent, true); // Ugly hack alert: @@ -740,7 +855,9 @@ public class StatusBarNotifier implements InCallPresenter.InCallStateListener { && callList.getBackgroundCall() != null)); if (isCallWaiting) { - Log.i(this, "updateInCallNotification: call-waiting! force relaunch..."); + LogUtil.i( + "StatusBarNotifier.configureFullScreenIntent", + "updateInCallNotification: call-waiting! force relaunch..."); // Cancel the IN_CALL_NOTIFICATION immediately before // (re)posting it; this seems to force the // NotificationManager to launch the fullScreenIntent. @@ -751,21 +868,15 @@ public class StatusBarNotifier implements InCallPresenter.InCallStateListener { private Notification.Builder getNotificationBuilder() { final Notification.Builder builder = new Notification.Builder(mContext); builder.setOngoing(true); - - // Make the notification prioritized over the other normal notifications. - builder.setPriority(Notification.PRIORITY_HIGH); + builder.setOnlyAlertOnce(true); return builder; } - private PendingIntent createLaunchPendingIntent(boolean isFullScreen, boolean isVideoCall) { + private PendingIntent createLaunchPendingIntent(boolean isFullScreen) { Intent intent = InCallActivity.getIntent( - mContext, - false /* showDialpad */, - false /* newOutgoingCall */, - isVideoCall, - isFullScreen); + mContext, false /* showDialpad */, false /* newOutgoingCall */, isFullScreen); int requestCode = PENDING_INTENT_REQUEST_CODE_NON_FULL_SCREEN; if (isFullScreen) { @@ -832,8 +943,9 @@ public class StatusBarNotifier implements InCallPresenter.InCallStateListener { * bar notification as required. */ @Override - public void onDialerCallSessionModificationStateChange(@SessionModificationState int state) { - if (state == DialerCall.SESSION_MODIFICATION_STATE_NO_REQUEST) { + public void onDialerCallSessionModificationStateChange() { + if (mDialerCall.getVideoTech().getSessionModificationState() + == VideoTech.SESSION_MODIFICATION_STATE_NO_REQUEST) { cleanup(); updateNotification(CallList.getInstance()); } diff --git a/java/com/android/incallui/VideoCallPresenter.java b/java/com/android/incallui/VideoCallPresenter.java index 971b6957a..20dc987da 100644 --- a/java/com/android/incallui/VideoCallPresenter.java +++ b/java/com/android/incallui/VideoCallPresenter.java @@ -21,7 +21,6 @@ import android.content.Context; import android.graphics.Point; import android.os.Handler; import android.support.annotation.Nullable; -import android.telecom.Connection; import android.telecom.InCallService.VideoCall; import android.telecom.VideoProfile; import android.telecom.VideoProfile.CameraCapabilities; @@ -36,17 +35,18 @@ import com.android.incallui.InCallPresenter.InCallStateListener; import com.android.incallui.InCallPresenter.IncomingCallListener; import com.android.incallui.call.CallList; import com.android.incallui.call.DialerCall; -import com.android.incallui.call.DialerCall.SessionModificationState; +import com.android.incallui.call.DialerCall.CameraDirection; import com.android.incallui.call.DialerCall.State; import com.android.incallui.call.InCallVideoCallCallbackNotifier; import com.android.incallui.call.InCallVideoCallCallbackNotifier.SurfaceChangeListener; -import com.android.incallui.call.InCallVideoCallCallbackNotifier.VideoEventListener; import com.android.incallui.call.VideoUtils; import com.android.incallui.util.AccessibilityUtil; import com.android.incallui.video.protocol.VideoCallScreen; import com.android.incallui.video.protocol.VideoCallScreenDelegate; import com.android.incallui.videosurface.protocol.VideoSurfaceDelegate; import com.android.incallui.videosurface.protocol.VideoSurfaceTexture; +import com.android.incallui.videotech.VideoTech; +import com.android.incallui.videotech.VideoTech.SessionModificationState; import java.util.Objects; /** @@ -78,7 +78,6 @@ public class VideoCallPresenter InCallStateListener, InCallDetailsListener, SurfaceChangeListener, - VideoEventListener, InCallPresenter.InCallEventListener, VideoCallScreenDelegate { @@ -90,32 +89,6 @@ public class VideoCallPresenter /** The current context. */ private Context mContext; - @Override - public boolean shouldShowCameraPermissionDialog() { - if (mPrimaryCall == null) { - LogUtil.i("VideoCallPresenter.shouldShowCameraPermissionDialog", "null call"); - return false; - } - if (mPrimaryCall.didShowCameraPermission()) { - LogUtil.i( - "VideoCallPresenter.shouldShowCameraPermissionDialog", "already shown for this call"); - return false; - } - if (!ConfigProviderBindings.get(mContext) - .getBoolean("camera_permission_dialog_allowed", true)) { - LogUtil.i("VideoCallPresenter.shouldShowCameraPermissionDialog", "disabled by config"); - return false; - } - return !VideoUtils.hasCameraPermission(mContext) || !VideoUtils.isCameraAllowedByUser(mContext); - } - - @Override - public void onCameraPermissionDialogShown() { - if (mPrimaryCall != null) { - mPrimaryCall.setDidShowCameraPermission(true); - } - } - /** The call the video surfaces are currently related to */ private DialerCall mPrimaryCall; /** @@ -231,49 +204,49 @@ public class VideoCallPresenter // this function should never be called with null call object, however if it happens we // should handle it gracefully. if (call == null) { - cameraDir = DialerCall.VideoSettings.CAMERA_DIRECTION_UNKNOWN; + cameraDir = CameraDirection.CAMERA_DIRECTION_UNKNOWN; LogUtil.e( "VideoCallPresenter.updateCameraSelection", "call is null. Setting camera direction to default value (CAMERA_DIRECTION_UNKNOWN)"); } // Clear camera direction if this is not a video call. - else if (VideoUtils.isAudioCall(call) && !isVideoUpgrade(call)) { - cameraDir = DialerCall.VideoSettings.CAMERA_DIRECTION_UNKNOWN; - call.getVideoSettings().setCameraDir(cameraDir); + else if (isAudioCall(call) && !isVideoUpgrade(call)) { + cameraDir = CameraDirection.CAMERA_DIRECTION_UNKNOWN; + call.setCameraDir(cameraDir); } // If this is a waiting video call, default to active call's camera, // since we don't want to change the current camera for waiting call // without user's permission. - else if (VideoUtils.isVideoCall(activeCall) && VideoUtils.isIncomingVideoCall(call)) { - cameraDir = activeCall.getVideoSettings().getCameraDir(); + else if (isVideoCall(activeCall) && isIncomingVideoCall(call)) { + cameraDir = activeCall.getCameraDir(); } // Infer the camera direction from the video state and store it, // if this is an outgoing video call. - else if (VideoUtils.isOutgoingVideoCall(call) && !isCameraDirectionSet(call)) { + else if (isOutgoingVideoCall(call) && !isCameraDirectionSet(call)) { cameraDir = toCameraDirection(call.getVideoState()); - call.getVideoSettings().setCameraDir(cameraDir); + call.setCameraDir(cameraDir); } // Use the stored camera dir if this is an outgoing video call for which camera direction // is set. - else if (VideoUtils.isOutgoingVideoCall(call)) { - cameraDir = call.getVideoSettings().getCameraDir(); + else if (isOutgoingVideoCall(call)) { + cameraDir = call.getCameraDir(); } // Infer the camera direction from the video state and store it, // if this is an active video call and camera direction is not set. - else if (VideoUtils.isActiveVideoCall(call) && !isCameraDirectionSet(call)) { + else if (isActiveVideoCall(call) && !isCameraDirectionSet(call)) { cameraDir = toCameraDirection(call.getVideoState()); - call.getVideoSettings().setCameraDir(cameraDir); + call.setCameraDir(cameraDir); } // Use the stored camera dir if this is an active video call for which camera direction // is set. - else if (VideoUtils.isActiveVideoCall(call)) { - cameraDir = call.getVideoSettings().getCameraDir(); + else if (isActiveVideoCall(call)) { + cameraDir = call.getCameraDir(); } // For all other cases infer the camera direction but don't store it in the call object. @@ -289,20 +262,18 @@ public class VideoCallPresenter final InCallCameraManager cameraManager = InCallPresenter.getInstance().getInCallCameraManager(); cameraManager.setUseFrontFacingCamera( - cameraDir == DialerCall.VideoSettings.CAMERA_DIRECTION_FRONT_FACING); + cameraDir == CameraDirection.CAMERA_DIRECTION_FRONT_FACING); } private static int toCameraDirection(int videoState) { return VideoProfile.isTransmissionEnabled(videoState) && !VideoProfile.isBidirectional(videoState) - ? DialerCall.VideoSettings.CAMERA_DIRECTION_BACK_FACING - : DialerCall.VideoSettings.CAMERA_DIRECTION_FRONT_FACING; + ? CameraDirection.CAMERA_DIRECTION_BACK_FACING + : CameraDirection.CAMERA_DIRECTION_FRONT_FACING; } private static boolean isCameraDirectionSet(DialerCall call) { - return VideoUtils.isVideoCall(call) - && call.getVideoSettings().getCameraDir() - != DialerCall.VideoSettings.CAMERA_DIRECTION_UNKNOWN; + return isVideoCall(call) && call.getCameraDir() != CameraDirection.CAMERA_DIRECTION_UNKNOWN; } private static String toSimpleString(DialerCall call) { @@ -350,7 +321,6 @@ public class VideoCallPresenter // Register for surface and video events from {@link InCallVideoCallListener}s. InCallVideoCallCallbackNotifier.getInstance().addSurfaceChangeListener(this); - InCallVideoCallCallbackNotifier.getInstance().addVideoEventListener(this); mCurrentVideoState = VideoProfile.STATE_AUDIO_ONLY; mCurrentCallState = DialerCall.State.INVALID; @@ -379,7 +349,6 @@ public class VideoCallPresenter InCallPresenter.getInstance().getLocalVideoSurfaceTexture().setDelegate(null); InCallVideoCallCallbackNotifier.getInstance().removeSurfaceChangeListener(this); - InCallVideoCallCallbackNotifier.getInstance().removeVideoEventListener(this); // Ensure that the call's camera direction is updated (most likely to UNKNOWN). Normally this // happens after any call state changes but we're unregistering from InCallPresenter above so @@ -447,7 +416,7 @@ public class VideoCallPresenter showVideoUi( mPrimaryCall.getVideoState(), mPrimaryCall.getState(), - mPrimaryCall.getSessionModificationState(), + mPrimaryCall.getVideoTech().getSessionModificationState(), mPrimaryCall.isRemotelyHeld()); InCallPresenter.getInstance().getInCallCameraManager().onCameraPermissionGranted(); } @@ -521,7 +490,7 @@ public class VideoCallPresenter // change the camera or UI unless the waiting VT call becomes active. primary = callList.getActiveCall(); currentCall = callList.getIncomingCall(); - if (!VideoUtils.isActiveVideoCall(primary)) { + if (!isActiveVideoCall(primary)) { primary = callList.getIncomingCall(); } } else if (newState == InCallPresenter.InCallState.OUTGOING) { @@ -564,10 +533,10 @@ public class VideoCallPresenter cancelAutoFullScreen(); if (mPrimaryCall != null) { updateFullscreenAndGreenScreenMode( - mPrimaryCall.getState(), mPrimaryCall.getSessionModificationState()); + mPrimaryCall.getState(), mPrimaryCall.getVideoTech().getSessionModificationState()); } else { updateFullscreenAndGreenScreenMode( - State.INVALID, DialerCall.SESSION_MODIFICATION_STATE_NO_REQUEST); + State.INVALID, VideoTech.SESSION_MODIFICATION_STATE_NO_REQUEST); } } @@ -622,7 +591,7 @@ public class VideoCallPresenter updateCameraSelection(call); String newCameraId = cameraManager.getActiveCameraId(); - if (!Objects.equals(prevCameraId, newCameraId) && VideoUtils.isActiveVideoCall(call)) { + if (!Objects.equals(prevCameraId, newCameraId) && isActiveVideoCall(call)) { enableCamera(call.getVideoCall(), true); } } @@ -631,7 +600,7 @@ public class VideoCallPresenter showVideoUi( call.getVideoState(), call.getState(), - call.getSessionModificationState(), + call.getVideoTech().getSessionModificationState(), call.isRemotelyHeld()); } @@ -711,12 +680,13 @@ public class VideoCallPresenter checkForVideoStateChange(call); checkForCallStateChange(call); checkForOrientationAllowedChange(call); - updateFullscreenAndGreenScreenMode(call.getState(), call.getSessionModificationState()); + updateFullscreenAndGreenScreenMode( + call.getState(), call.getVideoTech().getSessionModificationState()); } private void checkForOrientationAllowedChange(@Nullable DialerCall call) { InCallPresenter.getInstance() - .setInCallAllowsOrientationChange(VideoUtils.isVideoCall(call) || isVideoUpgrade(call)); + .setInCallAllowsOrientationChange(isVideoCall(call) || isVideoUpgrade(call)); } private void updateFullscreenAndGreenScreenMode( @@ -775,7 +745,8 @@ public class VideoCallPresenter private boolean isCameraRequired() { return mPrimaryCall != null && isCameraRequired( - mPrimaryCall.getVideoState(), mPrimaryCall.getSessionModificationState()); + mPrimaryCall.getVideoState(), + mPrimaryCall.getVideoTech().getSessionModificationState()); } /** @@ -799,7 +770,10 @@ public class VideoCallPresenter } showVideoUi( - newVideoState, call.getState(), call.getSessionModificationState(), call.isRemotelyHeld()); + newVideoState, + call.getState(), + call.getVideoTech().getSessionModificationState(), + call.isRemotelyHeld()); // Communicate the current camera to telephony and make a request for the camera // capabilities. @@ -814,7 +788,9 @@ public class VideoCallPresenter Assert.checkState( mDeviceOrientation != InCallOrientationEventListener.SCREEN_ORIENTATION_UNKNOWN); videoCall.setDeviceOrientation(mDeviceOrientation); - enableCamera(videoCall, isCameraRequired(newVideoState, call.getSessionModificationState())); + enableCamera( + videoCall, + isCameraRequired(newVideoState, call.getVideoTech().getSessionModificationState())); } int previousVideoState = mCurrentVideoState; mCurrentVideoState = newVideoState; @@ -822,7 +798,7 @@ public class VideoCallPresenter // adjustVideoMode may be called if we are already in a 1-way video state. In this case // we do not want to trigger auto-fullscreen mode. - if (!VideoUtils.isVideoCall(previousVideoState) && VideoUtils.isVideoCall(newVideoState)) { + if (!isVideoCall(previousVideoState) && isVideoCall(newVideoState)) { maybeAutoEnterFullscreen(call); } } @@ -832,7 +808,7 @@ public class VideoCallPresenter return false; } - if (VideoUtils.isVideoCall(call)) { + if (isVideoCall(call)) { return true; } @@ -877,7 +853,7 @@ public class VideoCallPresenter showVideoUi( VideoProfile.STATE_AUDIO_ONLY, DialerCall.State.ACTIVE, - DialerCall.SESSION_MODIFICATION_STATE_NO_REQUEST, + VideoTech.SESSION_MODIFICATION_STATE_NO_REQUEST, false /* isRemotelyHeld */); enableCamera(mVideoCall, false); InCallPresenter.getInstance().setFullScreen(false); @@ -917,20 +893,6 @@ public class VideoCallPresenter updateFullscreenAndGreenScreenMode(callState, sessionModificationState); } - /** - * Handles peer video pause state changes. - * - * @param call The call which paused or un-pausedvideo transmission. - * @param paused {@code True} when the video transmission is paused, {@code false} when video - * transmission resumes. - */ - @Override - public void onPeerPauseStateChanged(DialerCall call, boolean paused) { - if (!call.equals(mPrimaryCall)) { - return; - } - } - /** * Handles peer video dimension changes. * @@ -958,17 +920,6 @@ public class VideoCallPresenter } } - /** - * Handles any video quality changes in the call. - * - * @param call The call which experienced a video quality change. - * @param videoQuality The new video call quality. - */ - @Override - public void onVideoQualityChanged(DialerCall call, int videoQuality) { - // No-op - } - /** * Handles a change to the dimensions of the local camera. Receiving the camera capabilities * triggers the creation of the video @@ -1023,42 +974,6 @@ public class VideoCallPresenter mVideoCallScreen.onLocalVideoDimensionsChanged(); } - /** - * Called when call session event is raised. - * - * @param event The call session event. - */ - @Override - public void onCallSessionEvent(int event) { - switch (event) { - case Connection.VideoProvider.SESSION_EVENT_RX_PAUSE: - LogUtil.v("VideoCallPresenter.onCallSessionEvent", "rx_pause"); - break; - case Connection.VideoProvider.SESSION_EVENT_RX_RESUME: - LogUtil.v("VideoCallPresenter.onCallSessionEvent", "rx_resume"); - break; - case Connection.VideoProvider.SESSION_EVENT_CAMERA_FAILURE: - LogUtil.v("VideoCallPresenter.onCallSessionEvent", "camera_failure"); - break; - case Connection.VideoProvider.SESSION_EVENT_CAMERA_READY: - LogUtil.v("VideoCallPresenter.onCallSessionEvent", "camera_ready"); - break; - default: - LogUtil.v("VideoCallPresenter.onCallSessionEvent", "unknown event = : " + event); - break; - } - } - - /** - * Handles a change to the call data usage - * - * @param dataUsage call data usage value - */ - @Override - public void onCallDataUsageChange(long dataUsage) { - LogUtil.v("VideoCallPresenter.onCallDataUsageChange", "dataUsage=" + dataUsage); - } - /** * Handles changes to the device orientation. * @@ -1106,7 +1021,7 @@ public class VideoCallPresenter return; } - if (!VideoUtils.isVideoCall(call) || call.getState() == DialerCall.State.INCOMING) { + if (!isVideoCall(call) || call.getState() == DialerCall.State.INCOMING) { LogUtil.i("VideoCallPresenter.maybeExitFullscreen", "exiting fullscreen"); InCallPresenter.getInstance().setFullScreen(false); } @@ -1126,7 +1041,7 @@ public class VideoCallPresenter if (call == null || call.getState() != DialerCall.State.ACTIVE - || !VideoUtils.isBidirectionalVideoCall(call) + || !isBidirectionalVideoCall(call) || InCallPresenter.getInstance().isFullscreen() || (mContext != null && AccessibilityUtil.isTouchExplorationEnabled(mContext))) { // Ensure any previously scheduled attempt to enter fullscreen is cancelled. @@ -1156,6 +1071,32 @@ public class VideoCallPresenter mHandler.removeCallbacks(mAutoFullscreenRunnable); } + @Override + public boolean shouldShowCameraPermissionDialog() { + if (mPrimaryCall == null) { + LogUtil.i("VideoCallPresenter.shouldShowCameraPermissionDialog", "null call"); + return false; + } + if (mPrimaryCall.didShowCameraPermission()) { + LogUtil.i( + "VideoCallPresenter.shouldShowCameraPermissionDialog", "already shown for this call"); + return false; + } + if (!ConfigProviderBindings.get(mContext) + .getBoolean("camera_permission_dialog_allowed", true)) { + LogUtil.i("VideoCallPresenter.shouldShowCameraPermissionDialog", "disabled by config"); + return false; + } + return !VideoUtils.hasCameraPermission(mContext) || !VideoUtils.isCameraAllowedByUser(mContext); + } + + @Override + public void onCameraPermissionDialogShown() { + if (mPrimaryCall != null) { + mPrimaryCall.setDidShowCameraPermission(true); + } + } + private void updateRemoteVideoSurfaceDimensions() { Activity activity = mVideoCallScreen.getVideoCallScreenFragment().getActivity(); if (activity != null) { @@ -1166,8 +1107,8 @@ public class VideoCallPresenter } private static boolean isVideoUpgrade(DialerCall call) { - return VideoUtils.hasSentVideoUpgradeRequest(call) - || VideoUtils.hasReceivedVideoUpgradeRequest(call); + return call != null + && (call.hasSentVideoUpgradeRequest() || call.hasReceivedVideoUpgradeRequest()); } private static boolean isVideoUpgrade(@SessionModificationState int state) { @@ -1286,4 +1227,48 @@ public class VideoCallPresenter /** The surface has been set on the {@link VideoCall}. */ private static final int SURFACE_SET = 3; } + + private static boolean isBidirectionalVideoCall(DialerCall call) { + return CompatUtils.isVideoCompatible() && VideoProfile.isBidirectional(call.getVideoState()); + } + + private static boolean isIncomingVideoCall(DialerCall call) { + if (!isVideoCall(call)) { + return false; + } + final int state = call.getState(); + return (state == DialerCall.State.INCOMING) || (state == DialerCall.State.CALL_WAITING); + } + + private static boolean isActiveVideoCall(DialerCall call) { + return isVideoCall(call) && call.getState() == DialerCall.State.ACTIVE; + } + + private static boolean isOutgoingVideoCall(DialerCall call) { + if (!isVideoCall(call)) { + return false; + } + final int state = call.getState(); + return DialerCall.State.isDialing(state) + || state == DialerCall.State.CONNECTING + || state == DialerCall.State.SELECT_PHONE_ACCOUNT; + } + + private static boolean isAudioCall(DialerCall call) { + if (!CompatUtils.isVideoCompatible()) { + return true; + } + + return call != null && VideoProfile.isAudioOnly(call.getVideoState()); + } + + private static boolean isVideoCall(@Nullable DialerCall call) { + return call != null && call.isVideoCall(); + } + + private static boolean isVideoCall(int videoState) { + return CompatUtils.isVideoCompatible() + && (VideoProfile.isTransmissionEnabled(videoState) + || VideoProfile.isReceptionEnabled(videoState)); + } } diff --git a/java/com/android/incallui/VideoPauseController.java b/java/com/android/incallui/VideoPauseController.java index 2b4357704..2595e2f8b 100644 --- a/java/com/android/incallui/VideoPauseController.java +++ b/java/com/android/incallui/VideoPauseController.java @@ -17,14 +17,14 @@ package com.android.incallui; import android.support.annotation.NonNull; -import android.telecom.VideoProfile; +import com.android.dialer.common.Assert; +import com.android.dialer.common.LogUtil; import com.android.incallui.InCallPresenter.InCallState; import com.android.incallui.InCallPresenter.InCallStateListener; import com.android.incallui.InCallPresenter.IncomingCallListener; import com.android.incallui.call.CallList; import com.android.incallui.call.DialerCall; import com.android.incallui.call.DialerCall.State; -import com.android.incallui.call.VideoUtils; import java.util.Objects; /** @@ -32,12 +32,21 @@ import java.util.Objects; * to the background and subsequently brought back to the foreground. */ class VideoPauseController implements InCallStateListener, IncomingCallListener { - - private static final String TAG = "VideoPauseController"; private static VideoPauseController sVideoPauseController; private InCallPresenter mInCallPresenter; - /** The current call context, if applicable. */ - private CallContext mPrimaryCallContext = null; + + /** The current call, if applicable. */ + private DialerCall mPrimaryCall = null; + + /** + * The cached state of primary call, updated after onStateChange has processed. + * + *

These values are stored to detect specific changes in state between onStateChange calls. + */ + private int mPrevCallState = State.INVALID; + + private boolean mWasVideoCall = false; + /** * Tracks whether the application is in the background. {@code True} if the application is in the * background, {@code false} otherwise. @@ -57,51 +66,9 @@ class VideoPauseController implements InCallStateListener, IncomingCallListener return sVideoPauseController; } - /** - * Determines if a given call is the same one stored in a {@link CallContext}. - * - * @param call The call. - * @param callContext The call context. - * @return {@code true} if the {@link DialerCall} is the same as the one referenced in the {@link - * CallContext}. - */ - private static boolean areSame(DialerCall call, CallContext callContext) { - if (call == null && callContext == null) { - return true; - } else if (call == null || callContext == null) { - return false; - } - return call.equals(callContext.getCall()); - } - - /** - * Determines if a video call can be paused. Only a video call which is active can be paused. - * - * @param callContext The call context to check. - * @return {@code true} if the call is an active video call. - */ - private static boolean canVideoPause(CallContext callContext) { - return isVideoCall(callContext) && callContext.getState() == DialerCall.State.ACTIVE; - } - - /** - * Determines if a call referenced by a {@link CallContext} is a video call. - * - * @param callContext The call context. - * @return {@code true} if the call is a video call, {@code false} otherwise. - */ - private static boolean isVideoCall(CallContext callContext) { - return callContext != null && VideoUtils.isVideoCall(callContext.getVideoState()); - } - - /** - * Determines if call is in incoming/waiting state. - * - * @param call The call context. - * @return {@code true} if the call is in incoming or waiting state, {@code false} otherwise. - */ - private static boolean isIncomingCall(CallContext call) { - return call != null && isIncomingCall(call.getCall()); + private boolean wasIncomingCall() { + return (mPrevCallState == DialerCall.State.CALL_WAITING + || mPrevCallState == DialerCall.State.INCOMING); } /** @@ -119,11 +86,10 @@ class VideoPauseController implements InCallStateListener, IncomingCallListener /** * Determines if a call is dialing. * - * @param call The call context. * @return {@code true} if the call is dialing, {@code false} otherwise. */ - private static boolean isDialing(CallContext call) { - return call != null && DialerCall.State.isDialing(call.getState()); + private boolean wasDialing() { + return DialerCall.State.isDialing(mPrevCallState); } /** @@ -133,8 +99,8 @@ class VideoPauseController implements InCallStateListener, IncomingCallListener * @param inCallPresenter The {@link com.android.incallui.InCallPresenter}. */ public void setUp(@NonNull InCallPresenter inCallPresenter) { - log("setUp"); - mInCallPresenter = Objects.requireNonNull(inCallPresenter); + LogUtil.enterBlock("VideoPauseController.setUp"); + mInCallPresenter = Assert.isNotNull(inCallPresenter); mInCallPresenter.addListener(this); mInCallPresenter.addIncomingCallListener(this); } @@ -144,7 +110,7 @@ class VideoPauseController implements InCallStateListener, IncomingCallListener * state. Called from {@link com.android.incallui.InCallPresenter}. */ public void tearDown() { - log("tearDown..."); + LogUtil.enterBlock("VideoPauseController.tearDown"); mInCallPresenter.removeListener(this); mInCallPresenter.removeIncomingCallListener(this); clear(); @@ -153,7 +119,9 @@ class VideoPauseController implements InCallStateListener, IncomingCallListener /** Clears the internal state for the {@link VideoPauseController}. */ private void clear() { mInCallPresenter = null; - mPrimaryCallContext = null; + mPrimaryCall = null; + mPrevCallState = State.INVALID; + mWasVideoCall = false; mIsInBackground = false; } @@ -167,8 +135,6 @@ class VideoPauseController implements InCallStateListener, IncomingCallListener */ @Override public void onStateChange(InCallState oldState, InCallState newState, CallList callList) { - log("onStateChange, OldState=" + oldState + " NewState=" + newState); - DialerCall call; if (newState == InCallState.INCOMING) { call = callList.getIncomingCall(); @@ -182,22 +148,26 @@ class VideoPauseController implements InCallStateListener, IncomingCallListener call = callList.getActiveCall(); } - boolean hasPrimaryCallChanged = !areSame(call, mPrimaryCallContext); - boolean canVideoPause = VideoUtils.canVideoPause(call); - log("onStateChange, hasPrimaryCallChanged=" + hasPrimaryCallChanged); - log("onStateChange, canVideoPause=" + canVideoPause); - log("onStateChange, IsInBackground=" + mIsInBackground); + boolean hasPrimaryCallChanged = !Objects.equals(call, mPrimaryCall); + boolean canVideoPause = videoCanPause(call); + + LogUtil.i( + "VideoPauseController.onStateChange", + "hasPrimaryCallChanged: %b, videoCanPause: %b, isInBackground: %b", + hasPrimaryCallChanged, + canVideoPause, + mIsInBackground); if (hasPrimaryCallChanged) { onPrimaryCallChanged(call); return; } - if (isDialing(mPrimaryCallContext) && canVideoPause && mIsInBackground) { + if (wasDialing() && canVideoPause && mIsInBackground) { // Bring UI to foreground if outgoing request becomes active while UI is in // background. bringToForeground(); - } else if (!isVideoCall(mPrimaryCallContext) && canVideoPause && mIsInBackground) { + } else if (!mWasVideoCall && canVideoPause && mIsInBackground) { // Bring UI to foreground if VoLTE call becomes active while UI is in // background. bringToForeground(); @@ -216,27 +186,26 @@ class VideoPauseController implements InCallStateListener, IncomingCallListener * @param call The new primary call. */ private void onPrimaryCallChanged(DialerCall call) { - log("onPrimaryCallChanged: New call = " + call); - log("onPrimaryCallChanged: Old call = " + mPrimaryCallContext); - log("onPrimaryCallChanged, IsInBackground=" + mIsInBackground); - - if (areSame(call, mPrimaryCallContext)) { + LogUtil.i( + "VideoPauseController.onPrimaryCallChanged", + "new call: %s, old call: %s, mIsInBackground: %b", + call, + mPrimaryCall, + mIsInBackground); + + if (Objects.equals(call, mPrimaryCall)) { throw new IllegalStateException(); } - final boolean canVideoPause = VideoUtils.canVideoPause(call); + final boolean canVideoPause = videoCanPause(call); - if ((isIncomingCall(mPrimaryCallContext) - || isDialing(mPrimaryCallContext) - || (call != null && VideoProfile.isPaused(call.getVideoState()))) - && canVideoPause - && !mIsInBackground) { + if ((wasIncomingCall() || wasDialing()) && canVideoPause && !mIsInBackground) { // Send resume request for the active call, if user rejects incoming call, ends dialing // call, or the call was previously in a paused state and UI is in the foreground. sendRequest(call, true); - } else if (isIncomingCall(call) && canVideoPause(mPrimaryCallContext)) { + } else if (isIncomingCall(call) && videoCanPause(mPrimaryCall)) { // Send pause request if there is an active video call, and we just received a new // incoming call. - sendRequest(mPrimaryCallContext.getCall(), false); + sendRequest(mPrimaryCall, false); } updatePrimaryCallContext(call); @@ -251,9 +220,14 @@ class VideoPauseController implements InCallStateListener, IncomingCallListener */ @Override public void onIncomingCall(InCallState oldState, InCallState newState, DialerCall call) { - log("onIncomingCall, OldState=" + oldState + " NewState=" + newState + " DialerCall=" + call); - - if (areSame(call, mPrimaryCallContext)) { + LogUtil.i( + "VideoPauseController.onIncomingCall", + "oldState: %s, newState: %s, call: %s", + oldState, + newState, + call); + + if (Objects.equals(call, mPrimaryCall)) { return; } @@ -267,11 +241,13 @@ class VideoPauseController implements InCallStateListener, IncomingCallListener */ private void updatePrimaryCallContext(DialerCall call) { if (call == null) { - mPrimaryCallContext = null; - } else if (mPrimaryCallContext != null) { - mPrimaryCallContext.update(call); + mPrimaryCall = null; + mPrevCallState = State.INVALID; + mWasVideoCall = false; } else { - mPrimaryCallContext = new CallContext(call); + mPrimaryCall = call; + mPrevCallState = call.getState(); + mWasVideoCall = call.isVideoCall(); } } @@ -301,13 +277,9 @@ class VideoPauseController implements InCallStateListener, IncomingCallListener * video provider if we are in a call. */ private void onResume(boolean isInCall) { - log("onResume"); - mIsInBackground = false; - if (canVideoPause(mPrimaryCallContext) && isInCall) { - sendRequest(mPrimaryCallContext.getCall(), true); - } else { - log("onResume. Ignoring..."); + if (isInCall) { + sendRequest(mPrimaryCall, true); } } @@ -319,22 +291,20 @@ class VideoPauseController implements InCallStateListener, IncomingCallListener * video provider if we are in a call. */ private void onPause(boolean isInCall) { - log("onPause"); - mIsInBackground = true; - if (canVideoPause(mPrimaryCallContext) && isInCall) { - sendRequest(mPrimaryCallContext.getCall(), false); - } else { - log("onPause, Ignoring..."); + if (isInCall) { + sendRequest(mPrimaryCall, false); } } private void bringToForeground() { + LogUtil.enterBlock("VideoPauseController.bringToForeground"); if (mInCallPresenter != null) { - log("Bringing UI to foreground"); mInCallPresenter.bringToForeground(false); } else { - loge("InCallPresenter is null. Cannot bring UI to foreground"); + LogUtil.e( + "VideoPauseController.bringToForeground", + "InCallPresenter is null. Cannot bring UI to foreground"); } } @@ -345,72 +315,18 @@ class VideoPauseController implements InCallStateListener, IncomingCallListener * @param resume If true resume request will be sent, otherwise pause request. */ private void sendRequest(DialerCall call, boolean resume) { - // Check if this call supports pause/un-pause. - if (!call.can(android.telecom.Call.Details.CAPABILITY_CAN_PAUSE_VIDEO)) { + if (call == null) { return; } if (resume) { - log("sending resume request, call=" + call); - call.getVideoCall().sendSessionModifyRequest(VideoUtils.makeVideoUnPauseProfile(call)); + call.getVideoTech().unpause(); } else { - log("sending pause request, call=" + call); - call.getVideoCall().sendSessionModifyRequest(VideoUtils.makeVideoPauseProfile(call)); + call.getVideoTech().pause(); } } - /** - * Logs a debug message. - * - * @param msg The message. - */ - private void log(String msg) { - Log.d(this, TAG + msg); - } - - /** - * Logs an error message. - * - * @param msg The message. - */ - private void loge(String msg) { - Log.e(this, TAG + msg); - } - - /** Keeps track of the current active/foreground call. */ - private static class CallContext { - - private int mState = State.INVALID; - private int mVideoState; - private DialerCall mCall; - - public CallContext(@NonNull DialerCall call) { - Objects.requireNonNull(call); - update(call); - } - - public void update(@NonNull DialerCall call) { - mCall = Objects.requireNonNull(call); - mState = call.getState(); - mVideoState = call.getVideoState(); - } - - public int getState() { - return mState; - } - - public int getVideoState() { - return mVideoState; - } - - @Override - public String toString() { - return String.format( - "CallContext {CallId=%s, State=%s, VideoState=%d}", mCall.getId(), mState, mVideoState); - } - - public DialerCall getCall() { - return mCall; - } + private static boolean videoCanPause(DialerCall call) { + return call != null && call.isVideoCall() && call.getState() == DialerCall.State.ACTIVE; } } diff --git a/java/com/android/incallui/answer/bindings/AnswerBindings.java b/java/com/android/incallui/answer/bindings/AnswerBindings.java index f7a7a0a95..442e207a0 100644 --- a/java/com/android/incallui/answer/bindings/AnswerBindings.java +++ b/java/com/android/incallui/answer/bindings/AnswerBindings.java @@ -23,7 +23,7 @@ import com.android.incallui.answer.protocol.AnswerScreen; public class AnswerBindings { public static AnswerScreen createAnswerScreen( - String callId, int videoState, boolean isVideoUpgradeRequest) { - return AnswerFragment.newInstance(callId, videoState, isVideoUpgradeRequest); + String callId, boolean isVideoCall, boolean isVideoUpgradeRequest) { + return AnswerFragment.newInstance(callId, isVideoCall, isVideoUpgradeRequest); } } diff --git a/java/com/android/incallui/answer/impl/AnswerFragment.java b/java/com/android/incallui/answer/impl/AnswerFragment.java index 98439ee7f..6874daea3 100644 --- a/java/com/android/incallui/answer/impl/AnswerFragment.java +++ b/java/com/android/incallui/answer/impl/AnswerFragment.java @@ -37,7 +37,6 @@ import android.support.annotation.StringRes; import android.support.annotation.VisibleForTesting; import android.support.transition.TransitionManager; import android.support.v4.app.Fragment; -import android.telecom.VideoProfile; import android.text.TextUtils; import android.view.LayoutInflater; import android.view.View; @@ -79,10 +78,11 @@ import com.android.incallui.incall.protocol.InCallScreenDelegateFactory; import com.android.incallui.incall.protocol.PrimaryCallState; import com.android.incallui.incall.protocol.PrimaryInfo; import com.android.incallui.incall.protocol.SecondaryInfo; -import com.android.incallui.maps.StaticMapBinding; +import com.android.incallui.maps.MapsComponent; import com.android.incallui.sessiondata.AvatarPresenter; import com.android.incallui.sessiondata.MultimediaFragment; import com.android.incallui.util.AccessibilityUtil; +import com.android.incallui.video.protocol.VideoCallScreen; import java.util.ArrayList; import java.util.List; import java.util.Objects; @@ -101,7 +101,7 @@ public class AnswerFragment extends Fragment static final String ARG_CALL_ID = "call_id"; @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) - static final String ARG_VIDEO_STATE = "video_state"; + static final String ARG_IS_VIDEO_CALL = "is_video_call"; @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) static final String ARG_IS_VIDEO_UPGRADE_REQUEST = "is_video_upgrade_request"; @@ -143,7 +143,7 @@ public class AnswerFragment extends Fragment private CreateCustomSmsDialogFragment createCustomSmsDialogFragment; private SecondaryBehavior secondaryBehavior = SecondaryBehavior.REJECT_WITH_SMS; private ContactGridManager contactGridManager; - private AnswerVideoCallScreen answerVideoCallScreen; + private VideoCallScreen answerVideoCallScreen; private Handler handler = new Handler(Looper.getMainLooper()); private enum SecondaryBehavior { @@ -288,10 +288,10 @@ public class AnswerFragment extends Fragment } public static AnswerFragment newInstance( - String callId, int videoState, boolean isVideoUpgradeRequest) { + String callId, boolean isVideoCall, boolean isVideoUpgradeRequest) { Bundle bundle = new Bundle(); bundle.putString(ARG_CALL_ID, Assert.isNotNull(callId)); - bundle.putInt(ARG_VIDEO_STATE, videoState); + bundle.putBoolean(ARG_IS_VIDEO_CALL, isVideoCall); bundle.putBoolean(ARG_IS_VIDEO_UPGRADE_REQUEST, isVideoUpgradeRequest); AnswerFragment instance = new AnswerFragment(); @@ -305,11 +305,6 @@ public class AnswerFragment extends Fragment return Assert.isNotNull(getArguments().getString(ARG_CALL_ID)); } - @Override - public int getVideoState() { - return getArguments().getInt(ARG_VIDEO_STATE); - } - @Override public boolean isVideoUpgradeRequest() { return getArguments().getBoolean(ARG_IS_VIDEO_UPGRADE_REQUEST); @@ -317,7 +312,7 @@ public class AnswerFragment extends Fragment @Override public void setTextResponses(List textResponses) { - if (isVideoCall()) { + if (isVideoCall() || isVideoUpgradeRequest()) { LogUtil.i("AnswerFragment.setTextResponses", "no-op for video calls"); } else if (textResponses == null) { LogUtil.i("AnswerFragment.setTextResponses", "no text responses, hiding secondary button"); @@ -336,7 +331,9 @@ public class AnswerFragment extends Fragment private void initSecondaryButton() { secondaryBehavior = - isVideoCall() ? SecondaryBehavior.ANSWER_VIDEO_AS_AUDIO : SecondaryBehavior.REJECT_WITH_SMS; + isVideoCall() || isVideoUpgradeRequest() + ? SecondaryBehavior.ANSWER_VIDEO_AS_AUDIO + : SecondaryBehavior.REJECT_WITH_SMS; secondaryBehavior.applyToView(secondaryButton); secondaryButton.setOnClickListener( @@ -351,12 +348,9 @@ public class AnswerFragment extends Fragment secondaryButton.setAccessibilityDelegate(accessibilityDelegate); if (isVideoCall()) { - //noinspection WrongConstant - if (!isVideoUpgradeRequest() && VideoProfile.isTransmissionEnabled(getVideoState())) { - secondaryButton.setVisibility(View.VISIBLE); - } else { - secondaryButton.setVisibility(View.INVISIBLE); - } + secondaryButton.setVisibility(View.VISIBLE); + } else { + secondaryButton.setVisibility(View.INVISIBLE); } } @@ -448,11 +442,11 @@ public class AnswerFragment extends Fragment MultimediaData multimediaData = getSessionData(); if (multimediaData != null - && (!TextUtils.isEmpty(multimediaData.getSubject()) + && (!TextUtils.isEmpty(multimediaData.getText()) || (multimediaData.getImageUri() != null) || (multimediaData.getLocation() != null && canShowMap()))) { // Need message fragment - String subject = multimediaData.getSubject(); + String subject = multimediaData.getText(); Uri imageUri = multimediaData.getImageUri(); Location location = multimediaData.getLocation(); if (!(current instanceof MultimediaFragment) @@ -487,11 +481,11 @@ public class AnswerFragment extends Fragment } private boolean shouldShowAvatar() { - return !isVideoCall(); + return !isVideoCall() && !isVideoUpgradeRequest(); } private boolean canShowMap() { - return StaticMapBinding.get(getActivity().getApplication()) != null; + return MapsComponent.get(getContext()).getMaps().isAvailable(); } @Override @@ -564,7 +558,7 @@ public class AnswerFragment extends Fragment LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Bundle arguments = getArguments(); Assert.checkState(arguments.containsKey(ARG_CALL_ID)); - Assert.checkState(arguments.containsKey(ARG_VIDEO_STATE)); + Assert.checkState(arguments.containsKey(ARG_IS_VIDEO_CALL)); Assert.checkState(arguments.containsKey(ARG_IS_VIDEO_UPGRADE_REQUEST)); buttonAcceptClicked = false; @@ -596,7 +590,6 @@ public class AnswerFragment extends Fragment }); updateImportanceBadgeVisibility(); - boolean isVideoCall = isVideoCall(); contactGridManager = new ContactGridManager(view, null, 0, false /* showAnonymousAvatar */); Fragment answerMethod = @@ -625,9 +618,9 @@ public class AnswerFragment extends Fragment flags |= STATUS_BAR_DISABLE_BACK | STATUS_BAR_DISABLE_HOME | STATUS_BAR_DISABLE_RECENT; } view.setSystemUiVisibility(flags); - if (isVideoCall) { + if (isVideoCall() || isVideoUpgradeRequest()) { if (VideoUtils.hasCameraPermissionAndAllowedByUser(getContext())) { - answerVideoCallScreen = new AnswerVideoCallScreen(this, view); + answerVideoCallScreen = new AnswerVideoCallScreen(getCallId(), this, view); } else { view.findViewById(R.id.videocall_video_off).setVisibility(View.VISIBLE); } @@ -649,7 +642,7 @@ public class AnswerFragment extends Fragment updateUI(); if (savedInstanceState == null || !savedInstanceState.getBoolean(STATE_HAS_ANIMATED_ENTRY)) { - ViewUtil.doOnPreDraw(view, false, this::animateEntry); + ViewUtil.doOnGlobalLayout(view, this::animateEntry); } } @@ -667,7 +660,7 @@ public class AnswerFragment extends Fragment updateUI(); if (answerVideoCallScreen != null) { - answerVideoCallScreen.onStart(); + answerVideoCallScreen.onVideoScreenStart(); } } @@ -678,7 +671,7 @@ public class AnswerFragment extends Fragment handler.removeCallbacks(swipeHintRestoreTimer); if (answerVideoCallScreen != null) { - answerVideoCallScreen.onStop(); + answerVideoCallScreen.onVideoScreenStop(); } } @@ -722,7 +715,7 @@ public class AnswerFragment extends Fragment @Override public boolean isVideoCall() { - return VideoUtils.isVideoCall(getVideoState()); + return getArguments().getBoolean(ARG_IS_VIDEO_CALL); } @Override @@ -775,14 +768,12 @@ public class AnswerFragment extends Fragment Animator dataContainer = createTranslation(rootView.findViewById(R.id.incall_data_container)); AnimatorSet animatorSet = new AnimatorSet(); - animatorSet - .play(alpha) - .with(topRow) - .with(contactName) - .with(bottomRow) - .with(important) - .with(dataContainer); - animatorSet.setDuration(getResources().getInteger(android.R.integer.config_longAnimTime)); + AnimatorSet.Builder builder = animatorSet.play(alpha); + builder.with(topRow).with(contactName).with(bottomRow).with(important).with(dataContainer); + if (isShowingLocationUi()) { + builder.with(createTranslation(rootView.findViewById(R.id.incall_location_holder))); + } + animatorSet.setDuration(getResources().getInteger(R.integer.answer_animate_entry_millis)); animatorSet.addListener( new AnimatorListenerAdapter() { @Override @@ -803,14 +794,7 @@ public class AnswerFragment extends Fragment private void acceptCallByUser(boolean answerVideoAsAudio) { LogUtil.i("AnswerFragment.acceptCallByUser", answerVideoAsAudio ? " answerVideoAsAudio" : ""); if (!buttonAcceptClicked) { - int desiredVideoState = getVideoState(); - if (answerVideoAsAudio) { - desiredVideoState = VideoProfile.STATE_AUDIO_ONLY; - } - - // Notify the lower layer first to start signaling ASAP. - answerScreenDelegate.onAnswer(desiredVideoState); - + answerScreenDelegate.onAnswer(answerVideoAsAudio); buttonAcceptClicked = true; } } diff --git a/java/com/android/incallui/answer/impl/AnswerVideoCallScreen.java b/java/com/android/incallui/answer/impl/AnswerVideoCallScreen.java index 0316a5fab..06502daab 100644 --- a/java/com/android/incallui/answer/impl/AnswerVideoCallScreen.java +++ b/java/com/android/incallui/answer/impl/AnswerVideoCallScreen.java @@ -32,12 +32,15 @@ import com.android.incallui.videosurface.bindings.VideoSurfaceBindings; /** Shows a video preview for an incoming call. */ public class AnswerVideoCallScreen implements VideoCallScreen { + @NonNull private final String callId; @NonNull private final Fragment fragment; @NonNull private final TextureView textureView; @NonNull private final VideoCallScreenDelegate delegate; - public AnswerVideoCallScreen(@NonNull Fragment fragment, @NonNull View view) { - this.fragment = fragment; + public AnswerVideoCallScreen( + @NonNull String callId, @NonNull Fragment fragment, @NonNull View view) { + this.callId = Assert.isNotNull(callId); + this.fragment = Assert.isNotNull(fragment); textureView = Assert.isNotNull((TextureView) view.findViewById(R.id.incoming_preview_texture_view)); @@ -53,13 +56,15 @@ public class AnswerVideoCallScreen implements VideoCallScreen { overlayView.setVisibility(View.VISIBLE); } - public void onStart() { + @Override + public void onVideoScreenStart() { LogUtil.i("AnswerVideoCallScreen.onStart", null); delegate.onVideoCallScreenUiReady(); delegate.getLocalVideoSurfaceTexture().attachToTextureView(textureView); } - public void onStop() { + @Override + public void onVideoScreenStop() { LogUtil.i("AnswerVideoCallScreen.onStop", null); delegate.onVideoCallScreenUiUnready(); } @@ -98,6 +103,12 @@ public class AnswerVideoCallScreen implements VideoCallScreen { return fragment; } + @NonNull + @Override + public String getCallId() { + return callId; + } + private void updatePreviewVideoScaling() { if (textureView.getWidth() == 0 || textureView.getHeight() == 0) { LogUtil.i( diff --git a/java/com/android/incallui/answer/impl/affordance/SwipeButtonHelper.java b/java/com/android/incallui/answer/impl/affordance/SwipeButtonHelper.java index 62845b748..ff20d3a05 100644 --- a/java/com/android/incallui/answer/impl/affordance/SwipeButtonHelper.java +++ b/java/com/android/incallui/answer/impl/affordance/SwipeButtonHelper.java @@ -190,7 +190,7 @@ public class SwipeButtonHelper { case MotionEvent.ACTION_UP: isUp = true; - //fallthrough_intended + // fall through case MotionEvent.ACTION_CANCEL: boolean hintOnTheRight = targetedView == rightIcon; trackMovement(event); diff --git a/java/com/android/incallui/answer/impl/answermethod/AnswerMethodHolder.java b/java/com/android/incallui/answer/impl/answermethod/AnswerMethodHolder.java index 4052281b7..afa194f2e 100644 --- a/java/com/android/incallui/answer/impl/answermethod/AnswerMethodHolder.java +++ b/java/com/android/incallui/answer/impl/answermethod/AnswerMethodHolder.java @@ -44,4 +44,6 @@ public interface AnswerMethodHolder { * @return true iff the current call is a video call. */ boolean isVideoCall(); + + boolean isVideoUpgradeRequest(); } diff --git a/java/com/android/incallui/answer/impl/answermethod/FlingUpDownMethod.java b/java/com/android/incallui/answer/impl/answermethod/FlingUpDownMethod.java index 0bc65818c..6e8e1f7bf 100644 --- a/java/com/android/incallui/answer/impl/answermethod/FlingUpDownMethod.java +++ b/java/com/android/incallui/answer/impl/answermethod/FlingUpDownMethod.java @@ -60,7 +60,7 @@ import com.android.incallui.answer.impl.answermethod.FlingUpDownTouchHandler.OnP import com.android.incallui.answer.impl.classifier.FalsingManager; import com.android.incallui.answer.impl.hint.AnswerHint; import com.android.incallui.answer.impl.hint.AnswerHintFactory; -import com.android.incallui.answer.impl.hint.EventPayloadLoaderImpl; +import com.android.incallui.answer.impl.hint.PawImageLoaderImpl; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -228,7 +228,7 @@ public class FlingUpDownMethod extends AnswerMethod implements OnProgressChanged touchHandler = FlingUpDownTouchHandler.attach(view, this, falsingManager); answerHint = - new AnswerHintFactory(new EventPayloadLoaderImpl()) + new AnswerHintFactory(new PawImageLoaderImpl()) .create(getContext(), ANIMATE_DURATION_LONG_MILLIS, BOUNCE_ANIMATION_DELAY); answerHint.onCreateView( layoutInflater, @@ -319,7 +319,7 @@ public class FlingUpDownMethod extends AnswerMethod implements OnProgressChanged if (contactPuckIcon == null) { return; } - if (getParent().isVideoCall()) { + if (getParent().isVideoCall() || getParent().isVideoUpgradeRequest()) { contactPuckIcon.setImageResource(R.drawable.quantum_ic_videocam_white_24); } else { contactPuckIcon.setImageResource(R.drawable.quantum_ic_call_white_24); @@ -348,7 +348,8 @@ public class FlingUpDownMethod extends AnswerMethod implements OnProgressChanged } private boolean shouldShowPhotoInPuck() { - return getParent().isVideoCall() && contactPhoto != null; + return (getParent().isVideoCall() || getParent().isVideoUpgradeRequest()) + && contactPhoto != null; } @Override @@ -387,6 +388,10 @@ public class FlingUpDownMethod extends AnswerMethod implements OnProgressChanged // Since the animation progression is controlled by user gesture instead of real timeline, the // spec timeline can be divided into 9 slots. Each slot is equivalent to 83ms in the spec. // Therefore, we use 9 slots of 83ms to map user gesture into the spec timeline. + // + // See specs - + // Accept: https://direct.googleplex.com/#/spec/8510001 + // Decline: https://direct.googleplex.com/#/spec/3850001 final float progressSlots = 9; // Fade out the "swipe up to answer". It only takes 1 slot to complete the fade. @@ -414,7 +419,7 @@ public class FlingUpDownMethod extends AnswerMethod implements OnProgressChanged contactPuckBackground.setColorFilter(destPuckColor); // Animate decline icon - if (isAcceptingFlow || getParent().isVideoCall()) { + if (isAcceptingFlow || getParent().isVideoCall() || getParent().isVideoUpgradeRequest()) { rotateToward(contactPuckIcon, 0f); } else { rotateToward(contactPuckIcon, positiveAdjustedProgress * ICON_END_CALL_ROTATION_DEGREES); diff --git a/java/com/android/incallui/answer/impl/hint/AndroidManifest.xml b/java/com/android/incallui/answer/impl/hint/AndroidManifest.xml index b5fa6da8f..dfbba1cbf 100644 --- a/java/com/android/incallui/answer/impl/hint/AndroidManifest.xml +++ b/java/com/android/incallui/answer/impl/hint/AndroidManifest.xml @@ -3,7 +3,7 @@ xmlns:android="http://schemas.android.com/apk/res/android"> - + diff --git a/java/com/android/incallui/answer/impl/hint/AnswerHintFactory.java b/java/com/android/incallui/answer/impl/hint/AnswerHintFactory.java index 45395a71f..eaf5b74e5 100644 --- a/java/com/android/incallui/answer/impl/hint/AnswerHintFactory.java +++ b/java/com/android/incallui/answer/impl/hint/AnswerHintFactory.java @@ -28,7 +28,6 @@ import com.android.dialer.common.ConfigProvider; import com.android.dialer.common.ConfigProviderBindings; import com.android.dialer.common.LogUtil; import com.android.incallui.util.AccessibilityUtil; -import java.util.Calendar; /** * Selects a AnswerHint to show. If there's no suitable hints {@link EmptyAnswerHint} will be used, @@ -51,10 +50,10 @@ public class AnswerHintFactory { @VisibleForTesting static final String ANSWERED_COUNT_PREFERENCE_KEY = "answer_hint_answered_count"; - private final EventPayloadLoader eventPayloadLoader; + private final PawImageLoader pawImageLoader; - public AnswerHintFactory(@NonNull EventPayloadLoader eventPayloadLoader) { - this.eventPayloadLoader = Assert.isNotNull(eventPayloadLoader); + public AnswerHintFactory(@NonNull PawImageLoader pawImageLoader) { + this.pawImageLoader = Assert.isNotNull(pawImageLoader); } @NonNull @@ -69,11 +68,9 @@ public class AnswerHintFactory { } // Display the event answer hint if the payload is available. - Drawable eventPayload = - eventPayloadLoader.loadPayload( - context, System.currentTimeMillis(), Calendar.getInstance().getTimeZone()); + Drawable eventPayload = pawImageLoader.loadPayload(context); if (eventPayload != null) { - return new EventAnswerHint(context, eventPayload, puckUpDuration, puckUpDelay); + return new PawAnswerHint(context, eventPayload, puckUpDuration, puckUpDelay); } return new EmptyAnswerHint(); diff --git a/java/com/android/incallui/answer/impl/hint/EventAnswerHint.java b/java/com/android/incallui/answer/impl/hint/EventAnswerHint.java deleted file mode 100644 index 7ee327d50..000000000 --- a/java/com/android/incallui/answer/impl/hint/EventAnswerHint.java +++ /dev/null @@ -1,235 +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.incallui.answer.impl.hint; - -import android.animation.Animator; -import android.animation.AnimatorListenerAdapter; -import android.animation.AnimatorSet; -import android.animation.ObjectAnimator; -import android.content.Context; -import android.graphics.drawable.Drawable; -import android.support.annotation.DimenRes; -import android.support.annotation.NonNull; -import android.support.v4.view.animation.FastOutSlowInInterpolator; -import android.util.TypedValue; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.view.animation.Interpolator; -import android.view.animation.LinearInterpolator; -import android.widget.ImageView; -import android.widget.TextView; -import com.android.dialer.common.Assert; - -/** - * An Answer hint that animates a {@link Drawable} payload with animation similar to {@link - * DotAnswerHint}. - */ -public final class EventAnswerHint implements AnswerHint { - - private static final long FADE_IN_DELAY_SCALE_MILLIS = 380; - private static final long FADE_IN_DURATION_SCALE_MILLIS = 200; - private static final long FADE_IN_DELAY_ALPHA_MILLIS = 340; - private static final long FADE_IN_DURATION_ALPHA_MILLIS = 50; - - private static final long SWIPE_UP_DURATION_ALPHA_MILLIS = 500; - - private static final long FADE_OUT_DELAY_SCALE_SMALL_MILLIS = 90; - private static final long FADE_OUT_DURATION_SCALE_MILLIS = 100; - private static final long FADE_OUT_DELAY_ALPHA_MILLIS = 130; - private static final long FADE_OUT_DURATION_ALPHA_MILLIS = 170; - - private static final float FADE_SCALE = 1.2f; - - private final Context context; - private final Drawable payload; - private final long puckUpDurationMillis; - private final long puckUpDelayMillis; - - private View puck; - private View payloadView; - private View answerHintContainer; - private AnimatorSet answerGestureHintAnim; - - public EventAnswerHint( - @NonNull Context context, - @NonNull Drawable payload, - long puckUpDurationMillis, - long puckUpDelayMillis) { - this.context = Assert.isNotNull(context); - this.payload = Assert.isNotNull(payload); - this.puckUpDurationMillis = puckUpDurationMillis; - this.puckUpDelayMillis = puckUpDelayMillis; - } - - @Override - public void onCreateView( - LayoutInflater inflater, ViewGroup container, View puck, TextView hintText) { - this.puck = puck; - View view = inflater.inflate(R.layout.event_hint, container, true); - answerHintContainer = view.findViewById(R.id.answer_hint_container); - payloadView = view.findViewById(R.id.payload); - hintText.setTextSize( - TypedValue.COMPLEX_UNIT_PX, context.getResources().getDimension(R.dimen.hint_text_size)); - ((ImageView) payloadView).setImageDrawable(payload); - } - - @Override - public void onBounceStart() { - if (answerGestureHintAnim == null) { - - answerGestureHintAnim = new AnimatorSet(); - answerHintContainer.setY(puck.getY() + getDimension(R.dimen.hint_initial_offset)); - - Animator fadeIn = createFadeIn(); - - Animator swipeUp = - ObjectAnimator.ofFloat( - answerHintContainer, - View.TRANSLATION_Y, - puck.getY() - getDimension(R.dimen.hint_offset)); - swipeUp.setInterpolator(new FastOutSlowInInterpolator()); - swipeUp.setDuration(SWIPE_UP_DURATION_ALPHA_MILLIS); - - Animator fadeOut = createFadeOut(); - - answerGestureHintAnim.play(fadeIn).after(puckUpDelayMillis); - answerGestureHintAnim.play(swipeUp).after(fadeIn); - // The fade out should start fading the alpha just as the puck is dropping. Scaling will start - // a bit earlier. - answerGestureHintAnim - .play(fadeOut) - .after(puckUpDelayMillis + puckUpDurationMillis - FADE_OUT_DELAY_ALPHA_MILLIS); - - fadeIn.addListener( - new AnimatorListenerAdapter() { - @Override - public void onAnimationStart(Animator animation) { - super.onAnimationStart(animation); - payloadView.setAlpha(0); - payloadView.setScaleX(1); - payloadView.setScaleY(1); - answerHintContainer.setY(puck.getY() + getDimension(R.dimen.hint_initial_offset)); - answerHintContainer.setVisibility(View.VISIBLE); - } - }); - } - - answerGestureHintAnim.start(); - } - - private Animator createFadeIn() { - AnimatorSet set = new AnimatorSet(); - set.play(createFadeInScaleAndAlpha(payloadView)); - return set; - } - - private static Animator createFadeInScaleAndAlpha(View target) { - Animator scale = - createUniformScaleAnimator( - target, - FADE_SCALE, - 1.0f, - FADE_IN_DURATION_SCALE_MILLIS, - FADE_IN_DELAY_SCALE_MILLIS, - new LinearInterpolator()); - Animator alpha = - createAlphaAnimator( - target, - 0f, - 1.0f, - FADE_IN_DURATION_ALPHA_MILLIS, - FADE_IN_DELAY_ALPHA_MILLIS, - new LinearInterpolator()); - AnimatorSet set = new AnimatorSet(); - set.play(scale).with(alpha); - return set; - } - - private Animator createFadeOut() { - AnimatorSet set = new AnimatorSet(); - set.play(createFadeOutScaleAndAlpha(payloadView, FADE_OUT_DELAY_SCALE_SMALL_MILLIS)); - return set; - } - - private static Animator createFadeOutScaleAndAlpha(View target, long scaleDelay) { - Animator scale = - createUniformScaleAnimator( - target, - 1.0f, - FADE_SCALE, - FADE_OUT_DURATION_SCALE_MILLIS, - scaleDelay, - new LinearInterpolator()); - Animator alpha = - createAlphaAnimator( - target, - 01.0f, - 0.0f, - FADE_OUT_DURATION_ALPHA_MILLIS, - FADE_OUT_DELAY_ALPHA_MILLIS, - new LinearInterpolator()); - AnimatorSet set = new AnimatorSet(); - set.play(scale).with(alpha); - return set; - } - - @Override - public void onBounceEnd() { - if (answerGestureHintAnim != null) { - answerGestureHintAnim.end(); - answerGestureHintAnim = null; - answerHintContainer.setVisibility(View.GONE); - } - } - - @Override - public void onAnswered() { - // Do nothing - } - - private float getDimension(@DimenRes int id) { - return context.getResources().getDimension(id); - } - - private static Animator createUniformScaleAnimator( - View target, - float scaleBegin, - float scaleEnd, - long duration, - long delay, - Interpolator interpolator) { - Animator scaleX = ObjectAnimator.ofFloat(target, View.SCALE_X, scaleBegin, scaleEnd); - Animator scaleY = ObjectAnimator.ofFloat(target, View.SCALE_Y, scaleBegin, scaleEnd); - scaleX.setDuration(duration); - scaleY.setDuration(duration); - scaleX.setInterpolator(interpolator); - scaleY.setInterpolator(interpolator); - AnimatorSet set = new AnimatorSet(); - set.play(scaleX).with(scaleY).after(delay); - return set; - } - - private static Animator createAlphaAnimator( - View target, float begin, float end, long duration, long delay, Interpolator interpolator) { - Animator alpha = ObjectAnimator.ofFloat(target, View.ALPHA, begin, end); - alpha.setDuration(duration); - alpha.setInterpolator(interpolator); - alpha.setStartDelay(delay); - return alpha; - } -} diff --git a/java/com/android/incallui/answer/impl/hint/EventPayloadLoader.java b/java/com/android/incallui/answer/impl/hint/EventPayloadLoader.java deleted file mode 100644 index 09e3bedf2..000000000 --- a/java/com/android/incallui/answer/impl/hint/EventPayloadLoader.java +++ /dev/null @@ -1,30 +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.incallui.answer.impl.hint; - -import android.content.Context; -import android.graphics.drawable.Drawable; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import java.util.TimeZone; - -/** Loads a {@link Drawable} payload for the {@link EventAnswerHint} if it should be displayed. */ -public interface EventPayloadLoader { - @Nullable - Drawable loadPayload( - @NonNull Context context, long currentTimeUtcMillis, @NonNull TimeZone timeZone); -} diff --git a/java/com/android/incallui/answer/impl/hint/EventPayloadLoaderImpl.java b/java/com/android/incallui/answer/impl/hint/EventPayloadLoaderImpl.java deleted file mode 100644 index bd8d73645..000000000 --- a/java/com/android/incallui/answer/impl/hint/EventPayloadLoaderImpl.java +++ /dev/null @@ -1,118 +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.incallui.answer.impl.hint; - -import android.annotation.TargetApi; -import android.content.Context; -import android.content.SharedPreferences; -import android.graphics.BitmapFactory; -import android.graphics.drawable.BitmapDrawable; -import android.graphics.drawable.Drawable; -import android.os.Build.VERSION_CODES; -import android.preference.PreferenceManager; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.support.annotation.VisibleForTesting; -import com.android.dialer.common.Assert; -import com.android.dialer.common.ConfigProvider; -import com.android.dialer.common.ConfigProviderBindings; -import com.android.dialer.common.LogUtil; -import java.io.InputStream; -import java.util.TimeZone; -import javax.crypto.Cipher; -import javax.crypto.SecretKey; -import javax.crypto.SecretKeyFactory; -import javax.crypto.spec.PBEKeySpec; - -/** Decrypt the event payload to be shown if in a specific time range and the key is received. */ -@TargetApi(VERSION_CODES.M) -public final class EventPayloadLoaderImpl implements EventPayloadLoader { - - @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) - static final String CONFIG_EVENT_KEY = "event_key"; - - @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) - static final String CONFIG_EVENT_BINARY = "event_binary"; - - // Time is stored as a UTC UNIX timestamp in milliseconds, but interpreted as local time. - // For example, 946684800 (2000/1/1 00:00:00 @UTC) is the new year midnight at every timezone. - @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) - static final String CONFIG_EVENT_START_UTC_AS_LOCAL_MILLIS = "event_time_start_millis"; - - @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) - static final String CONFIG_EVENT_TIME_END_UTC_AS_LOCAL_MILLIS = "event_time_end_millis"; - - @Override - @Nullable - public Drawable loadPayload( - @NonNull Context context, long currentTimeUtcMillis, @NonNull TimeZone timeZone) { - Assert.isNotNull(context); - Assert.isNotNull(timeZone); - ConfigProvider configProvider = ConfigProviderBindings.get(context); - - String pbeKey = configProvider.getString(CONFIG_EVENT_KEY, null); - if (pbeKey == null) { - return null; - } - long timeRangeStart = configProvider.getLong(CONFIG_EVENT_START_UTC_AS_LOCAL_MILLIS, 0); - long timeRangeEnd = configProvider.getLong(CONFIG_EVENT_TIME_END_UTC_AS_LOCAL_MILLIS, 0); - - String eventBinary = configProvider.getString(CONFIG_EVENT_BINARY, null); - if (eventBinary == null) { - return null; - } - - SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); - if (!preferences.getBoolean( - EventSecretCodeListener.EVENT_ENABLED_WITH_SECRET_CODE_KEY, false)) { - long localTimestamp = currentTimeUtcMillis + timeZone.getRawOffset(); - - if (localTimestamp < timeRangeStart) { - return null; - } - - if (localTimestamp > timeRangeEnd) { - return null; - } - } - - // Use openssl aes-128-cbc -in -out -pass to generate the asset - try (InputStream input = context.getAssets().open(eventBinary)) { - byte[] encryptedFile = new byte[input.available()]; - input.read(encryptedFile); - - Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding", "BC"); - - byte[] salt = new byte[8]; - System.arraycopy(encryptedFile, 8, salt, 0, 8); - SecretKey key = - SecretKeyFactory.getInstance("PBEWITHMD5AND128BITAES-CBC-OPENSSL", "BC") - .generateSecret(new PBEKeySpec(pbeKey.toCharArray(), salt, 100)); - cipher.init(Cipher.DECRYPT_MODE, key); - - byte[] decryptedFile = cipher.doFinal(encryptedFile, 16, encryptedFile.length - 16); - - return new BitmapDrawable( - context.getResources(), - BitmapFactory.decodeByteArray(decryptedFile, 0, decryptedFile.length)); - } catch (Exception e) { - // Avoid crashing dialer for any reason. - LogUtil.e("EventPayloadLoader.loadPayload", "error decrypting payload:", e); - return null; - } - } -} diff --git a/java/com/android/incallui/answer/impl/hint/EventSecretCodeListener.java b/java/com/android/incallui/answer/impl/hint/EventSecretCodeListener.java deleted file mode 100644 index 7cf4054a9..000000000 --- a/java/com/android/incallui/answer/impl/hint/EventSecretCodeListener.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * 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.incallui.answer.impl.hint; - -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; -import android.content.SharedPreferences; -import android.preference.PreferenceManager; -import android.support.annotation.VisibleForTesting; -import android.text.TextUtils; -import android.widget.Toast; -import com.android.dialer.common.ConfigProviderBindings; -import com.android.dialer.common.LogUtil; -import com.android.dialer.logging.Logger; -import com.android.dialer.logging.nano.DialerImpression; - -/** - * Listen to the broadcast when the user dials "*#*#[number]#*#*" to toggle the event answer hint. - */ -public class EventSecretCodeListener extends BroadcastReceiver { - - @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) - static final String CONFIG_EVENT_SECRET_CODE = "event_secret_code"; - - public static final String EVENT_ENABLED_WITH_SECRET_CODE_KEY = "event_enabled_with_secret_code"; - - @Override - public void onReceive(Context context, Intent intent) { - String host = intent.getData().getHost(); - String secretCode = - ConfigProviderBindings.get(context).getString(CONFIG_EVENT_SECRET_CODE, null); - if (secretCode == null) { - return; - } - if (!TextUtils.equals(secretCode, host)) { - return; - } - SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); - boolean wasEnabled = preferences.getBoolean(EVENT_ENABLED_WITH_SECRET_CODE_KEY, false); - if (wasEnabled) { - preferences.edit().putBoolean(EVENT_ENABLED_WITH_SECRET_CODE_KEY, false).apply(); - Toast.makeText(context, R.string.event_deactivated, Toast.LENGTH_SHORT).show(); - Logger.get(context).logImpression(DialerImpression.Type.EVENT_ANSWER_HINT_DEACTIVATED); - LogUtil.i("EventSecretCodeListener.onReceive", "EventAnswerHint disabled"); - } else { - preferences.edit().putBoolean(EVENT_ENABLED_WITH_SECRET_CODE_KEY, true).apply(); - Toast.makeText(context, R.string.event_activated, Toast.LENGTH_SHORT).show(); - Logger.get(context).logImpression(DialerImpression.Type.EVENT_ANSWER_HINT_ACTIVATED); - LogUtil.i("EventSecretCodeListener.onReceive", "EventAnswerHint enabled"); - } - } -} diff --git a/java/com/android/incallui/answer/impl/hint/PawAnswerHint.java b/java/com/android/incallui/answer/impl/hint/PawAnswerHint.java new file mode 100644 index 000000000..36b761f57 --- /dev/null +++ b/java/com/android/incallui/answer/impl/hint/PawAnswerHint.java @@ -0,0 +1,236 @@ +/* + * 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.incallui.answer.impl.hint; + +import android.animation.Animator; +import android.animation.AnimatorListenerAdapter; +import android.animation.AnimatorSet; +import android.animation.ObjectAnimator; +import android.content.Context; +import android.graphics.drawable.Drawable; +import android.support.annotation.DimenRes; +import android.support.annotation.NonNull; +import android.support.v4.view.animation.FastOutSlowInInterpolator; +import android.util.TypedValue; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.view.animation.Interpolator; +import android.view.animation.LinearInterpolator; +import android.widget.ImageView; +import android.widget.TextView; +import com.android.dialer.common.Assert; + +/** + * An Answer hint that animates a {@link Drawable} payload with animation similar to {@link + * DotAnswerHint}. + */ +public final class PawAnswerHint implements AnswerHint { + + private static final long FADE_IN_DELAY_SCALE_MILLIS = 380; + private static final long FADE_IN_DURATION_SCALE_MILLIS = 200; + private static final long FADE_IN_DELAY_ALPHA_MILLIS = 340; + private static final long FADE_IN_DURATION_ALPHA_MILLIS = 50; + + private static final long SWIPE_UP_DURATION_ALPHA_MILLIS = 500; + + private static final long FADE_OUT_DELAY_SCALE_SMALL_MILLIS = 90; + private static final long FADE_OUT_DURATION_SCALE_MILLIS = 100; + private static final long FADE_OUT_DELAY_ALPHA_MILLIS = 130; + private static final long FADE_OUT_DURATION_ALPHA_MILLIS = 170; + + private static final float IMAGE_SCALE = 1.5f; + private static final float FADE_SCALE = 2.0f; + + private final Context context; + private final Drawable payload; + private final long puckUpDurationMillis; + private final long puckUpDelayMillis; + + private View puck; + private View payloadView; + private View answerHintContainer; + private AnimatorSet answerGestureHintAnim; + + public PawAnswerHint( + @NonNull Context context, + @NonNull Drawable payload, + long puckUpDurationMillis, + long puckUpDelayMillis) { + this.context = Assert.isNotNull(context); + this.payload = Assert.isNotNull(payload); + this.puckUpDurationMillis = puckUpDurationMillis; + this.puckUpDelayMillis = puckUpDelayMillis; + } + + @Override + public void onCreateView( + LayoutInflater inflater, ViewGroup container, View puck, TextView hintText) { + this.puck = puck; + View view = inflater.inflate(R.layout.paw_hint, container, true); + answerHintContainer = view.findViewById(R.id.answer_hint_container); + payloadView = view.findViewById(R.id.paw_image); + hintText.setTextSize( + TypedValue.COMPLEX_UNIT_PX, context.getResources().getDimension(R.dimen.hint_text_size)); + ((ImageView) payloadView).setImageDrawable(payload); + } + + @Override + public void onBounceStart() { + if (answerGestureHintAnim == null) { + + answerGestureHintAnim = new AnimatorSet(); + answerHintContainer.setY(puck.getY() + getDimension(R.dimen.hint_initial_offset)); + + Animator fadeIn = createFadeIn(); + + Animator swipeUp = + ObjectAnimator.ofFloat( + answerHintContainer, + View.TRANSLATION_Y, + puck.getY() - getDimension(R.dimen.hint_offset)); + swipeUp.setInterpolator(new FastOutSlowInInterpolator()); + swipeUp.setDuration(SWIPE_UP_DURATION_ALPHA_MILLIS); + + Animator fadeOut = createFadeOut(); + + answerGestureHintAnim.play(fadeIn).after(puckUpDelayMillis); + answerGestureHintAnim.play(swipeUp).after(fadeIn); + // The fade out should start fading the alpha just as the puck is dropping. Scaling will start + // a bit earlier. + answerGestureHintAnim + .play(fadeOut) + .after(puckUpDelayMillis + puckUpDurationMillis - FADE_OUT_DELAY_ALPHA_MILLIS); + + fadeIn.addListener( + new AnimatorListenerAdapter() { + @Override + public void onAnimationStart(Animator animation) { + super.onAnimationStart(animation); + payloadView.setAlpha(0); + payloadView.setScaleX(1); + payloadView.setScaleY(1); + answerHintContainer.setY(puck.getY() + getDimension(R.dimen.hint_initial_offset)); + answerHintContainer.setVisibility(View.VISIBLE); + } + }); + } + + answerGestureHintAnim.start(); + } + + private Animator createFadeIn() { + AnimatorSet set = new AnimatorSet(); + set.play(createFadeInScaleAndAlpha(payloadView)); + return set; + } + + private static Animator createFadeInScaleAndAlpha(View target) { + Animator scale = + createUniformScaleAnimator( + target, + FADE_SCALE, + IMAGE_SCALE, + FADE_IN_DURATION_SCALE_MILLIS, + FADE_IN_DELAY_SCALE_MILLIS, + new LinearInterpolator()); + Animator alpha = + createAlphaAnimator( + target, + 0f, + 1.0f, + FADE_IN_DURATION_ALPHA_MILLIS, + FADE_IN_DELAY_ALPHA_MILLIS, + new LinearInterpolator()); + AnimatorSet set = new AnimatorSet(); + set.play(scale).with(alpha); + return set; + } + + private Animator createFadeOut() { + AnimatorSet set = new AnimatorSet(); + set.play(createFadeOutScaleAndAlpha(payloadView, FADE_OUT_DELAY_SCALE_SMALL_MILLIS)); + return set; + } + + private static Animator createFadeOutScaleAndAlpha(View target, long scaleDelay) { + Animator scale = + createUniformScaleAnimator( + target, + IMAGE_SCALE, + FADE_SCALE, + FADE_OUT_DURATION_SCALE_MILLIS, + scaleDelay, + new LinearInterpolator()); + Animator alpha = + createAlphaAnimator( + target, + 1.0f, + 0.0f, + FADE_OUT_DURATION_ALPHA_MILLIS, + FADE_OUT_DELAY_ALPHA_MILLIS, + new LinearInterpolator()); + AnimatorSet set = new AnimatorSet(); + set.play(scale).with(alpha); + return set; + } + + @Override + public void onBounceEnd() { + if (answerGestureHintAnim != null) { + answerGestureHintAnim.end(); + answerGestureHintAnim = null; + answerHintContainer.setVisibility(View.GONE); + } + } + + @Override + public void onAnswered() { + // Do nothing + } + + private float getDimension(@DimenRes int id) { + return context.getResources().getDimension(id); + } + + private static Animator createUniformScaleAnimator( + View target, + float scaleBegin, + float scaleEnd, + long duration, + long delay, + Interpolator interpolator) { + Animator scaleX = ObjectAnimator.ofFloat(target, View.SCALE_X, scaleBegin, scaleEnd); + Animator scaleY = ObjectAnimator.ofFloat(target, View.SCALE_Y, scaleBegin, scaleEnd); + scaleX.setDuration(duration); + scaleY.setDuration(duration); + scaleX.setInterpolator(interpolator); + scaleY.setInterpolator(interpolator); + AnimatorSet set = new AnimatorSet(); + set.play(scaleX).with(scaleY).after(delay); + return set; + } + + private static Animator createAlphaAnimator( + View target, float begin, float end, long duration, long delay, Interpolator interpolator) { + Animator alpha = ObjectAnimator.ofFloat(target, View.ALPHA, begin, end); + alpha.setDuration(duration); + alpha.setInterpolator(interpolator); + alpha.setStartDelay(delay); + return alpha; + } +} diff --git a/java/com/android/incallui/answer/impl/hint/PawImageLoader.java b/java/com/android/incallui/answer/impl/hint/PawImageLoader.java new file mode 100644 index 000000000..09e700fe0 --- /dev/null +++ b/java/com/android/incallui/answer/impl/hint/PawImageLoader.java @@ -0,0 +1,28 @@ +/* + * 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.incallui.answer.impl.hint; + +import android.content.Context; +import android.graphics.drawable.Drawable; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; + +/** Loads a {@link Drawable} payload for the {@link PawAnswerHint} if it should be displayed. */ +public interface PawImageLoader { + @Nullable + Drawable loadPayload(@NonNull Context context); +} diff --git a/java/com/android/incallui/answer/impl/hint/PawImageLoaderImpl.java b/java/com/android/incallui/answer/impl/hint/PawImageLoaderImpl.java new file mode 100644 index 000000000..485a9ae37 --- /dev/null +++ b/java/com/android/incallui/answer/impl/hint/PawImageLoaderImpl.java @@ -0,0 +1,48 @@ +/* + * 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.incallui.answer.impl.hint; + +import android.annotation.TargetApi; +import android.content.Context; +import android.content.SharedPreferences; +import android.graphics.drawable.Drawable; +import android.os.Build.VERSION_CODES; +import android.preference.PreferenceManager; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import com.android.dialer.common.Assert; + +/** Decrypt the event payload to be shown if in a specific time range and the key is received. */ +@TargetApi(VERSION_CODES.M) +public final class PawImageLoaderImpl implements PawImageLoader { + + @Override + @Nullable + public Drawable loadPayload(@NonNull Context context) { + Assert.isNotNull(context); + + SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); + if (!preferences.getBoolean(PawSecretCodeListener.PAW_ENABLED_WITH_SECRET_CODE_KEY, false)) { + return null; + } + int drawableId = preferences.getInt(PawSecretCodeListener.PAW_DRAWABLE_ID_KEY, 0); + if (drawableId == 0) { + return null; + } + return context.getDrawable(drawableId); + } +} diff --git a/java/com/android/incallui/answer/impl/hint/PawSecretCodeListener.java b/java/com/android/incallui/answer/impl/hint/PawSecretCodeListener.java new file mode 100644 index 000000000..b4fc19c0d --- /dev/null +++ b/java/com/android/incallui/answer/impl/hint/PawSecretCodeListener.java @@ -0,0 +1,81 @@ +/* + * 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.incallui.answer.impl.hint; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.SharedPreferences; +import android.preference.PreferenceManager; +import android.support.annotation.VisibleForTesting; +import android.text.TextUtils; +import android.widget.Toast; +import com.android.dialer.common.Assert; +import com.android.dialer.common.ConfigProviderBindings; +import com.android.dialer.common.LogUtil; +import com.android.dialer.logging.Logger; +import com.android.dialer.logging.nano.DialerImpression.Type; +import java.util.Random; + +/** + * Listen to the broadcast when the user dials "*#*#[number]#*#*" to toggle the event answer hint. + */ +public class PawSecretCodeListener extends BroadcastReceiver { + + @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) + static final String CONFIG_PAW_SECRET_CODE = "paw_secret_code"; + + public static final String PAW_ENABLED_WITH_SECRET_CODE_KEY = "paw_enabled_with_secret_code"; + public static final String PAW_DRAWABLE_ID_KEY = "paw_drawable_id"; + + @Override + public void onReceive(Context context, Intent intent) { + String host = intent.getData().getHost(); + Assert.checkState(!TextUtils.isEmpty(host)); + String secretCode = + ConfigProviderBindings.get(context).getString(CONFIG_PAW_SECRET_CODE, "729"); + if (secretCode == null) { + return; + } + if (!TextUtils.equals(secretCode, host)) { + return; + } + SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); + boolean wasEnabled = preferences.getBoolean(PAW_ENABLED_WITH_SECRET_CODE_KEY, false); + if (wasEnabled) { + preferences.edit().putBoolean(PAW_ENABLED_WITH_SECRET_CODE_KEY, false).apply(); + Toast.makeText(context, R.string.event_deactivated, Toast.LENGTH_SHORT).show(); + Logger.get(context).logImpression(Type.EVENT_ANSWER_HINT_DEACTIVATED); + LogUtil.i("PawSecretCodeListener.onReceive", "PawAnswerHint disabled"); + } else { + int drawableId; + if (new Random().nextBoolean()) { + drawableId = R.drawable.cat_paw; + } else { + drawableId = R.drawable.dog_paw; + } + preferences + .edit() + .putBoolean(PAW_ENABLED_WITH_SECRET_CODE_KEY, true) + .putInt(PAW_DRAWABLE_ID_KEY, drawableId) + .apply(); + Toast.makeText(context, R.string.event_activated, Toast.LENGTH_SHORT).show(); + Logger.get(context).logImpression(Type.EVENT_ANSWER_HINT_ACTIVATED); + LogUtil.i("PawSecretCodeListener.onReceive", "PawAnswerHint enabled"); + } + } +} diff --git a/java/com/android/incallui/answer/impl/hint/res/drawable-xxhdpi/cat_paw.webp b/java/com/android/incallui/answer/impl/hint/res/drawable-xxhdpi/cat_paw.webp new file mode 100644 index 000000000..f7ff6eb54 Binary files /dev/null and b/java/com/android/incallui/answer/impl/hint/res/drawable-xxhdpi/cat_paw.webp differ diff --git a/java/com/android/incallui/answer/impl/hint/res/drawable-xxhdpi/dog_paw.webp b/java/com/android/incallui/answer/impl/hint/res/drawable-xxhdpi/dog_paw.webp new file mode 100644 index 000000000..3a232542c Binary files /dev/null and b/java/com/android/incallui/answer/impl/hint/res/drawable-xxhdpi/dog_paw.webp differ diff --git a/java/com/android/incallui/answer/impl/hint/res/layout/event_hint.xml b/java/com/android/incallui/answer/impl/hint/res/layout/event_hint.xml deleted file mode 100644 index d505014c1..000000000 --- a/java/com/android/incallui/answer/impl/hint/res/layout/event_hint.xml +++ /dev/null @@ -1,36 +0,0 @@ - - - - - \ No newline at end of file diff --git a/java/com/android/incallui/answer/impl/hint/res/layout/paw_hint.xml b/java/com/android/incallui/answer/impl/hint/res/layout/paw_hint.xml new file mode 100644 index 000000000..c3b12a01d --- /dev/null +++ b/java/com/android/incallui/answer/impl/hint/res/layout/paw_hint.xml @@ -0,0 +1,37 @@ + + + + + \ No newline at end of file diff --git a/java/com/android/incallui/answer/impl/proguard.flags b/java/com/android/incallui/answer/impl/proguard.flags new file mode 100644 index 000000000..016352857 --- /dev/null +++ b/java/com/android/incallui/answer/impl/proguard.flags @@ -0,0 +1,5 @@ +# Used in com.android.dialer.answer.impl.SmsBottomSheetFragment +-keep class android.support.design.widget.BottomSheetBehavior { + public (android.content.Context, android.util.AttributeSet); + public (); +} \ No newline at end of file diff --git a/java/com/android/incallui/answer/impl/res/values/dimens.xml b/java/com/android/incallui/answer/impl/res/values/dimens.xml index c48b68f93..8329707a6 100644 --- a/java/com/android/incallui/answer/impl/res/values/dimens.xml +++ b/java/com/android/incallui/answer/impl/res/values/dimens.xml @@ -22,4 +22,5 @@ 0dp 0dp false + 1000 diff --git a/java/com/android/incallui/answer/protocol/AnswerScreen.java b/java/com/android/incallui/answer/protocol/AnswerScreen.java index 0c374eb7f..f03efefc4 100644 --- a/java/com/android/incallui/answer/protocol/AnswerScreen.java +++ b/java/com/android/incallui/answer/protocol/AnswerScreen.java @@ -24,7 +24,7 @@ public interface AnswerScreen { String getCallId(); - int getVideoState(); + boolean isVideoCall(); boolean isVideoUpgradeRequest(); diff --git a/java/com/android/incallui/answer/protocol/AnswerScreenDelegate.java b/java/com/android/incallui/answer/protocol/AnswerScreenDelegate.java index 9934497cf..36b4e3a6b 100644 --- a/java/com/android/incallui/answer/protocol/AnswerScreenDelegate.java +++ b/java/com/android/incallui/answer/protocol/AnswerScreenDelegate.java @@ -27,7 +27,7 @@ public interface AnswerScreenDelegate { void onRejectCallWithMessage(String message); - void onAnswer(int videoState); + void onAnswer(boolean answerVideoAsAudio); void onReject(); diff --git a/java/com/android/incallui/answerproximitysensor/AnswerProximitySensor.java b/java/com/android/incallui/answerproximitysensor/AnswerProximitySensor.java index edc3db34b..6a2c4b493 100644 --- a/java/com/android/incallui/answerproximitysensor/AnswerProximitySensor.java +++ b/java/com/android/incallui/answerproximitysensor/AnswerProximitySensor.java @@ -23,7 +23,6 @@ import android.view.Display; import com.android.dialer.common.ConfigProviderBindings; import com.android.dialer.common.LogUtil; import com.android.incallui.call.DialerCall; -import com.android.incallui.call.DialerCall.SessionModificationState; import com.android.incallui.call.DialerCall.State; import com.android.incallui.call.DialerCallListener; @@ -141,7 +140,7 @@ public class AnswerProximitySensor public void onHandoverToWifiFailure() {} @Override - public void onDialerCallSessionModificationStateChange(@SessionModificationState int state) {} + public void onDialerCallSessionModificationStateChange() {} @Override public void onScreenOn() { diff --git a/java/com/android/incallui/call/CallList.java b/java/com/android/incallui/call/CallList.java index 862c71cf9..c88802f14 100644 --- a/java/com/android/incallui/call/CallList.java +++ b/java/com/android/incallui/call/CallList.java @@ -38,10 +38,10 @@ import com.android.dialer.logging.nano.DialerImpression; import com.android.dialer.shortcuts.ShortcutUsageReporter; import com.android.dialer.spam.Spam; import com.android.dialer.spam.SpamBindings; -import com.android.incallui.call.DialerCall.SessionModificationState; import com.android.incallui.call.DialerCall.State; import com.android.incallui.latencyreport.LatencyReport; import com.android.incallui.util.TelecomCallUtil; +import com.android.incallui.videotech.VideoTech; import java.util.Collections; import java.util.Iterator; import java.util.Map; @@ -110,6 +110,8 @@ public class CallList implements DialerCallDelegate { Trace.beginSection("onCallAdded"); final DialerCall call = new DialerCall(context, this, telecomCall, latencyReport, true /* registerCallback */); + logSecondIncomingCall(context, call); + final DialerCallListenerImpl dialerCallListener = new DialerCallListenerImpl(call); call.addListener(dialerCallListener); LogUtil.d("CallList.onCallAdded", "callState=" + call.getState()); @@ -184,6 +186,30 @@ public class CallList implements DialerCallDelegate { Trace.endSection(); } + private void logSecondIncomingCall(@NonNull Context context, @NonNull DialerCall incomingCall) { + DialerCall firstCall = getFirstCall(); + if (firstCall != null) { + int impression = 0; + if (firstCall.isVideoCall()) { + if (incomingCall.isVideoCall()) { + impression = DialerImpression.Type.VIDEO_CALL_WITH_INCOMING_VIDEO_CALL; + } else { + impression = DialerImpression.Type.VIDEO_CALL_WITH_INCOMING_VOICE_CALL; + } + } else { + if (incomingCall.isVideoCall()) { + impression = DialerImpression.Type.VOICE_CALL_WITH_INCOMING_VIDEO_CALL; + } else { + impression = DialerImpression.Type.VOICE_CALL_WITH_INCOMING_VOICE_CALL; + } + } + Assert.checkArgument(impression != 0); + Logger.get(context) + .logCallImpression( + impression, incomingCall.getUniqueCallId(), incomingCall.getTimeAddedMs()); + } + } + private static boolean isPotentialEmergencyCallback(Context context, DialerCall call) { if (BuildCompat.isAtLeastO()) { return call.isPotentialEmergencyCallback(); @@ -440,8 +466,8 @@ public class CallList implements DialerCallDelegate { */ public DialerCall getVideoUpgradeRequestCall() { for (DialerCall call : mCallById.values()) { - if (call.getSessionModificationState() - == DialerCall.SESSION_MODIFICATION_STATE_RECEIVED_UPGRADE_TO_VIDEO_REQUEST) { + if (call.getVideoTech().getSessionModificationState() + == VideoTech.SESSION_MODIFICATION_STATE_RECEIVED_UPGRADE_TO_VIDEO_REQUEST) { return call; } } @@ -637,17 +663,7 @@ public class CallList implements DialerCallDelegate { */ public void notifyCallsOfDeviceRotation(int rotation) { for (DialerCall call : mCallById.values()) { - // First, ensure that the call videoState has video enabled (there is no need to set - // device orientation on a voice call which has not yet been upgraded to video). - // Second, ensure a VideoCall is set on the call so that the change can be sent to the - // provider (a VideoCall can be present for a call that does not currently have video, - // but can be upgraded to video). - - // NOTE: is it necessary to use this order because getVideoCall references the class - // VideoProfile which is not available on APIs <23 (M). - if (VideoUtils.isVideoCall(call) && call.getVideoCall() != null) { - call.getVideoCall().setDeviceOrientation(rotation); - } + call.getVideoTech().setDeviceOrientation(rotation); } } @@ -675,7 +691,7 @@ public class CallList implements DialerCallDelegate { void onUpgradeToVideo(DialerCall call); /** Called when the session modification state of a call changes. */ - void onSessionModificationStateChange(@SessionModificationState int newState); + void onSessionModificationStateChange(DialerCall call); /** * Called anytime there are changes to the call list. The change can be switching call states, @@ -754,9 +770,9 @@ public class CallList implements DialerCallDelegate { } @Override - public void onDialerCallSessionModificationStateChange(@SessionModificationState int state) { + public void onDialerCallSessionModificationStateChange() { for (Listener listener : mListeners) { - listener.onSessionModificationStateChange(state); + listener.onSessionModificationStateChange(mCall); } } } diff --git a/java/com/android/incallui/call/DialerCall.java b/java/com/android/incallui/call/DialerCall.java index bd8f006dd..15a0233e8 100644 --- a/java/com/android/incallui/call/DialerCall.java +++ b/java/com/android/incallui/call/DialerCall.java @@ -24,6 +24,7 @@ import android.os.Build.VERSION_CODES; import android.os.Bundle; import android.os.Trace; import android.support.annotation.IntDef; +import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.telecom.Call; import android.telecom.Call.Details; @@ -47,10 +48,15 @@ import com.android.dialer.callintent.nano.CallSpecificAppData; import com.android.dialer.common.Assert; import com.android.dialer.common.ConfigProviderBindings; import com.android.dialer.common.LogUtil; +import com.android.dialer.enrichedcall.EnrichedCallComponent; import com.android.dialer.logging.nano.ContactLookupResult; -import com.android.dialer.util.CallUtil; import com.android.incallui.latencyreport.LatencyReport; import com.android.incallui.util.TelecomCallUtil; +import com.android.incallui.videotech.VideoTech; +import com.android.incallui.videotech.VideoTech.VideoTechListener; +import com.android.incallui.videotech.empty.EmptyVideoTech; +import com.android.incallui.videotech.ims.ImsVideoTech; +import com.android.incallui.videotech.rcs.RcsVideoShare; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.util.ArrayList; @@ -62,11 +68,16 @@ import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.TimeUnit; /** Describes a single call and its state. */ -public class DialerCall { +public class DialerCall implements VideoTechListener { public static final int CALL_HISTORY_STATUS_UNKNOWN = 0; public static final int CALL_HISTORY_STATUS_PRESENT = 1; public static final int CALL_HISTORY_STATUS_NOT_PRESENT = 2; + + // Hard coded property for {@code Call}. Upstreamed change from Motorola. + // TODO(b/35359461): Move it to Telecom in framework. + public static final int PROPERTY_CODEC_KNOWN = 0x04000000; + private static final String ID_PREFIX = "DialerCall_"; private static final String CONFIG_EMERGENCY_CALLBACK_WINDOW_MILLIS = "emergency_callback_window_millis"; @@ -82,13 +93,13 @@ public class DialerCall { private final LatencyReport mLatencyReport; private final String mId; private final List mChildCallIds = new ArrayList<>(); - private final VideoSettings mVideoSettings = new VideoSettings(); private final LogState mLogState = new LogState(); private final Context mContext; private final DialerCallDelegate mDialerCallDelegate; private final List mListeners = new CopyOnWriteArrayList<>(); private final List mCannedTextResponsesLoadedListeners = new CopyOnWriteArrayList<>(); + private final VideoTechManager mVideoTechManager; private boolean mIsEmergencyCall; private Uri mHandle; @@ -98,13 +109,6 @@ public class DialerCall { private boolean hasShownWiFiToLteHandoverToast; private boolean doNotShowDialogForHandoffToWifiFailure; - @SessionModificationState private int mSessionModificationState; - private int mVideoState; - /** mRequestedVideoState is used to store requested upgrade / downgrade video state */ - private int mRequestedVideoState = VideoProfile.STATE_AUDIO_ONLY; - - private InCallVideoCallCallback mVideoCallCallback; - private boolean mIsVideoCallCallbackRegistered; private String mChildNumber; private String mLastForwardedNumber; private String mCallSubject; @@ -118,6 +122,7 @@ public class DialerCall { private boolean didShowCameraPermission; private String callProviderLabel; private String callbackNumber; + private int mCameraDirection = CameraDirection.CAMERA_DIRECTION_UNKNOWN; public static String getNumberFromHandle(Uri handle) { return handle == null ? "" : handle.getSchemeSpecificPart(); @@ -125,7 +130,7 @@ public class DialerCall { /** * Whether the call is put on hold by remote party. This is different than the {@link - * State.ONHOLD} state which indicates that the call is being held locally on the device. + * State#ONHOLD} state which indicates that the call is being held locally on the device. */ private boolean isRemotelyHeld; @@ -189,7 +194,7 @@ public class DialerCall { @Override public void onCallDestroyed(Call call) { LogUtil.v("TelecomCallCallback.onStateChanged", "call=" + call); - call.unregisterCallback(this); + unregisterCallback(); } @Override @@ -248,7 +253,10 @@ public class DialerCall { mLatencyReport = latencyReport; mId = ID_PREFIX + Integer.toString(sIdCounter++); - updateFromTelecomCall(registerCallback); + // Must be after assigning mTelecomCall + mVideoTechManager = new VideoTechManager(this); + + updateFromTelecomCall(); if (registerCallback) { mTelecomCall.registerCallback(mTelecomCallCallback); @@ -348,19 +356,24 @@ public class DialerCall { return mTelecomCall.getDetails().getStatusHints(); } - /** - * @return video settings of the call, null if the call is not a video call. - * @see VideoProfile - */ - public VideoSettings getVideoSettings() { - return mVideoSettings; + public int getCameraDir() { + return mCameraDirection; + } + + public void setCameraDir(int cameraDir) { + if (cameraDir == CameraDirection.CAMERA_DIRECTION_FRONT_FACING + || cameraDir == CameraDirection.CAMERA_DIRECTION_BACK_FACING) { + mCameraDirection = cameraDir; + } else { + mCameraDirection = CameraDirection.CAMERA_DIRECTION_UNKNOWN; + } } private void update() { Trace.beginSection("Update"); int oldState = getState(); // We want to potentially register a video call callback here. - updateFromTelecomCall(true /* registerCallback */); + updateFromTelecomCall(); if (oldState != getState() && getState() == DialerCall.State.DISCONNECTED) { for (DialerCallListener listener : mListeners) { listener.onDialerCallDisconnect(); @@ -373,21 +386,15 @@ public class DialerCall { Trace.endSection(); } - private void updateFromTelecomCall(boolean registerCallback) { + private void updateFromTelecomCall() { LogUtil.v("DialerCall.updateFromTelecomCall", mTelecomCall.toString()); + + mVideoTechManager.dispatchCallStateChanged(mTelecomCall.getState()); + final int translatedState = translateState(mTelecomCall.getState()); if (mState != State.BLOCKED) { setState(translatedState); setDisconnectCause(mTelecomCall.getDetails().getDisconnectCause()); - maybeCancelVideoUpgrade(mTelecomCall.getDetails().getVideoState()); - } - - if (registerCallback && mTelecomCall.getVideoCall() != null) { - if (mVideoCallCallback == null) { - mVideoCallCallback = new InCallVideoCallCallback(this); - } - mTelecomCall.getVideoCall().registerCallback(mVideoCallCallback); - mIsVideoCallCallbackRegistered = true; } mChildCallIds.clear(); @@ -428,19 +435,6 @@ public class DialerCall { } } } - - if (mSessionModificationState - == DialerCall.SESSION_MODIFICATION_STATE_WAITING_FOR_UPGRADE_TO_VIDEO_RESPONSE - && isVideoCall()) { - // We find out in {@link InCallVideoCallCallback.onSessionModifyResponseReceived} - // whether the video upgrade request was accepted. We don't clear the session modification - // state right away though to avoid having the UI switch from video to voice to video. - // Once the underlying telecom call updates to video mode it's safe to clear the state. - LogUtil.i( - "DialerCall.updateFromTelecomCall", - "upgraded to video, clearing session modification state"); - setSessionModificationState(DialerCall.SESSION_MODIFICATION_STATE_NO_REQUEST); - } } /** @@ -518,25 +512,6 @@ public class DialerCall { } } - /** - * Determines if a received upgrade to video request should be cancelled. This can happen if - * another InCall UI responds to the upgrade to video request. - * - * @param newVideoState The new video state. - */ - private void maybeCancelVideoUpgrade(int newVideoState) { - boolean isVideoStateChanged = mVideoState != newVideoState; - - if (mSessionModificationState - == DialerCall.SESSION_MODIFICATION_STATE_RECEIVED_UPGRADE_TO_VIDEO_REQUEST - && isVideoStateChanged) { - - LogUtil.i("DialerCall.maybeCancelVideoUpgrade", "cancelling upgrade notification"); - setSessionModificationState(DialerCall.SESSION_MODIFICATION_STATE_NO_REQUEST); - } - mVideoState = newVideoState; - } - public String getId() { return mId; } @@ -710,6 +685,7 @@ public class DialerCall { return mTelecomCall.getDetails().hasProperty(property); } + @NonNull public String getUniqueCallId() { return uniqueCallId; } @@ -733,15 +709,9 @@ public class DialerCall { return mTelecomCall == null ? null : mTelecomCall.getDetails().getAccountHandle(); } - /** - * @return The {@link VideoCall} instance associated with the {@link Call}. Will return {@code - * null} until {@link #updateFromTelecomCall(boolean)} has registered a valid callback on the - * {@link VideoCall}. - */ + /** @return The {@link VideoCall} instance associated with the {@link Call}. */ public VideoCall getVideoCall() { - return mTelecomCall == null || !mIsVideoCallCallbackRegistered - ? null - : mTelecomCall.getVideoCall(); + return mTelecomCall == null ? null : mTelecomCall.getVideoCall(); } public List getChildCallIds() { @@ -761,85 +731,23 @@ public class DialerCall { } public boolean isVideoCall() { - return CallUtil.isVideoEnabled(mContext) && VideoUtils.isVideoCall(getVideoState()); + return getVideoTech().isTransmittingOrReceiving(); } - /** - * Determines if the call handle is an emergency number or not and caches the result to avoid - * repeated calls to isEmergencyNumber. - */ - private void updateEmergencyCallState() { - mIsEmergencyCall = TelecomCallUtil.isEmergencyCall(mTelecomCall); + public boolean hasReceivedVideoUpgradeRequest() { + return VideoUtils.hasReceivedVideoUpgradeRequest(getVideoTech().getSessionModificationState()); } - /** - * Gets the video state which was requested via a session modification request. - * - * @return The video state. - */ - public int getRequestedVideoState() { - return mRequestedVideoState; + public boolean hasSentVideoUpgradeRequest() { + return VideoUtils.hasSentVideoUpgradeRequest(getVideoTech().getSessionModificationState()); } /** - * Handles incoming session modification requests. Stores the pending video request and sets the - * session modification state to {@link - * DialerCall#SESSION_MODIFICATION_STATE_RECEIVED_UPGRADE_TO_VIDEO_REQUEST} so that we can keep - * track of the fact the request was received. Only upgrade requests require user confirmation and - * will be handled by this method. The remote user can turn off their own camera without - * confirmation. - * - * @param videoState The requested video state. - */ - public void setRequestedVideoState(int videoState) { - LogUtil.v("DialerCall.setRequestedVideoState", "videoState: " + videoState); - if (videoState == getVideoState()) { - LogUtil.e("DialerCall.setRequestedVideoState", "clearing session modification state"); - setSessionModificationState(DialerCall.SESSION_MODIFICATION_STATE_NO_REQUEST); - return; - } - - mRequestedVideoState = videoState; - setSessionModificationState( - DialerCall.SESSION_MODIFICATION_STATE_RECEIVED_UPGRADE_TO_VIDEO_REQUEST); - for (DialerCallListener listener : mListeners) { - listener.onDialerCallUpgradeToVideo(); - } - - LogUtil.i( - "DialerCall.setRequestedVideoState", - "mSessionModificationState: %d, videoState: %d", - mSessionModificationState, - videoState); - update(); - } - - /** - * Gets the current video session modification state. - * - * @return The session modification state. - */ - @SessionModificationState - public int getSessionModificationState() { - return mSessionModificationState; - } - - /** - * Set the session modification state. Used to keep track of pending video session modification - * operations and to inform listeners of these changes. - * - * @param state the new session modification state. + * Determines if the call handle is an emergency number or not and caches the result to avoid + * repeated calls to isEmergencyNumber. */ - public void setSessionModificationState(@SessionModificationState int state) { - boolean hasChanged = mSessionModificationState != state; - if (hasChanged) { - LogUtil.i( - "DialerCall.setSessionModificationState", "%d -> %d", mSessionModificationState, state); - mSessionModificationState = state; - for (DialerCallListener listener : mListeners) { - listener.onDialerCallSessionModificationStateChange(state); - } - } + private void updateEmergencyCallState() { + mIsEmergencyCall = TelecomCallUtil.isEmergencyCall(mTelecomCall); } public LogState getLogState() { @@ -861,24 +769,6 @@ public class DialerCall { && hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL); } - /** - * Determines if the external call is pullable. - * - *

An external call is one which does not exist locally for the {@link - * android.telecom.ConnectionService} it is associated with. An external call may be "pullable", - * which means that the user can request it be transferred to the current device. - * - *

External calls are only supported in N and higher. - * - * @return {@code true} if the call is an external call, {@code false} otherwise. - */ - public boolean isPullableExternalCall() { - return VERSION.SDK_INT >= VERSION_CODES.N - && (mTelecomCall.getDetails().getCallCapabilities() - & CallCompat.Details.CAPABILITY_CAN_PULL_CALL) - == CallCompat.Details.CAPABILITY_CAN_PULL_CALL; - } - /** * Determines if answering this call will cause an ongoing video call to be dropped. * @@ -922,7 +812,7 @@ public class DialerCall { return String.format( Locale.US, "[%s, %s, %s, %s, children:%s, parent:%s, " - + "conferenceable:%s, videoState:%s, mSessionModificationState:%d, VideoSettings:%s]", + + "conferenceable:%s, videoState:%s, mSessionModificationState:%d, CameraDir:%s]", mId, State.toString(getState()), Details.capabilitiesToString(mTelecomCall.getDetails().getCallCapabilities()), @@ -931,8 +821,8 @@ public class DialerCall { getParentId(), this.mTelecomCall.getConferenceableCalls(), VideoProfile.videoStateToString(mTelecomCall.getDetails().getVideoState()), - mSessionModificationState, - getVideoSettings()); + getVideoTech().getSessionModificationState(), + getCameraDir()); } public String toSimpleString() { @@ -1012,20 +902,6 @@ public class DialerCall { mTelecomCall.unregisterCallback(mTelecomCallCallback); } - public void acceptUpgradeRequest(int videoState) { - LogUtil.i("DialerCall.acceptUpgradeRequest", "videoState: " + videoState); - VideoProfile videoProfile = new VideoProfile(videoState); - getVideoCall().sendSessionModifyResponse(videoProfile); - setSessionModificationState(DialerCall.SESSION_MODIFICATION_STATE_NO_REQUEST); - } - - public void declineUpgradeRequest() { - LogUtil.i("DialerCall.declineUpgradeRequest", ""); - VideoProfile videoProfile = new VideoProfile(getVideoState()); - getVideoCall().sendSessionModifyResponse(videoProfile); - setSessionModificationState(DialerCall.SESSION_MODIFICATION_STATE_NO_REQUEST); - } - public void phoneAccountSelected(PhoneAccountHandle accountHandle, boolean setDefault) { LogUtil.i( "DialerCall.phoneAccountSelected", @@ -1064,6 +940,10 @@ public class DialerCall { mTelecomCall.answer(videoState); } + public void answer() { + answer(mTelecomCall.getDetails().getVideoState()); + } + public void reject(boolean rejectWithMessage, String message) { LogUtil.i("DialerCall.reject", ""); mTelecomCall.reject(rejectWithMessage, message); @@ -1095,6 +975,10 @@ public class DialerCall { return mContext.getSystemService(TelecomManager.class).getPhoneAccount(accountHandle); } + public VideoTech getVideoTech() { + return mVideoTechManager.getVideoTech(); + } + public String getCallbackNumber() { if (callbackNumber == null) { // Show the emergency callback number if either: @@ -1146,6 +1030,39 @@ public class DialerCall { return null; } + @Override + public void onVideoTechStateChanged() { + update(); + } + + @Override + public void onSessionModificationStateChanged() { + for (DialerCallListener listener : mListeners) { + listener.onDialerCallSessionModificationStateChange(); + } + } + + @Override + public void onCameraDimensionsChanged(int width, int height) { + InCallVideoCallCallbackNotifier.getInstance().cameraDimensionsChanged(this, width, height); + } + + @Override + public void onPeerDimensionsChanged(int width, int height) { + InCallVideoCallCallbackNotifier.getInstance().peerDimensionsChanged(this, width, height); + } + + @Override + public void onVideoUpgradeRequestReceived() { + LogUtil.enterBlock("DialerCall.onVideoUpgradeRequestReceived"); + + for (DialerCallListener listener : mListeners) { + listener.onDialerCallUpgradeToVideo(); + } + + update(); + } + /** * Specifies whether a number is in the call history or not. {@link #CALL_HISTORY_STATUS_UNKNOWN} * means there is no result. @@ -1191,8 +1108,8 @@ public class DialerCall { case CONFERENCED: return true; default: + return false; } - return false; } public static boolean isDialing(int state) { @@ -1239,71 +1156,11 @@ public class DialerCall { } } - /** - * Defines different states of session modify requests, which are used to upgrade to video, or - * downgrade to audio. - */ - @Retention(RetentionPolicy.SOURCE) - @IntDef({ - SESSION_MODIFICATION_STATE_NO_REQUEST, - SESSION_MODIFICATION_STATE_WAITING_FOR_UPGRADE_TO_VIDEO_RESPONSE, - SESSION_MODIFICATION_STATE_REQUEST_FAILED, - SESSION_MODIFICATION_STATE_RECEIVED_UPGRADE_TO_VIDEO_REQUEST, - SESSION_MODIFICATION_STATE_UPGRADE_TO_VIDEO_REQUEST_TIMED_OUT, - SESSION_MODIFICATION_STATE_UPGRADE_TO_VIDEO_REQUEST_FAILED, - SESSION_MODIFICATION_STATE_REQUEST_REJECTED, - SESSION_MODIFICATION_STATE_WAITING_FOR_RESPONSE - }) - public @interface SessionModificationState {} - - public static final int SESSION_MODIFICATION_STATE_NO_REQUEST = 0; - public static final int SESSION_MODIFICATION_STATE_WAITING_FOR_UPGRADE_TO_VIDEO_RESPONSE = 1; - public static final int SESSION_MODIFICATION_STATE_REQUEST_FAILED = 2; - public static final int SESSION_MODIFICATION_STATE_RECEIVED_UPGRADE_TO_VIDEO_REQUEST = 3; - public static final int SESSION_MODIFICATION_STATE_UPGRADE_TO_VIDEO_REQUEST_TIMED_OUT = 4; - public static final int SESSION_MODIFICATION_STATE_UPGRADE_TO_VIDEO_REQUEST_FAILED = 5; - public static final int SESSION_MODIFICATION_STATE_REQUEST_REJECTED = 6; - public static final int SESSION_MODIFICATION_STATE_WAITING_FOR_RESPONSE = 7; - - public static class VideoSettings { - + /** Camera direction constants */ + public static class CameraDirection { public static final int CAMERA_DIRECTION_UNKNOWN = -1; public static final int CAMERA_DIRECTION_FRONT_FACING = CameraCharacteristics.LENS_FACING_FRONT; public static final int CAMERA_DIRECTION_BACK_FACING = CameraCharacteristics.LENS_FACING_BACK; - - private int mCameraDirection = CAMERA_DIRECTION_UNKNOWN; - - /** - * Gets the camera direction. if camera direction is set to CAMERA_DIRECTION_UNKNOWN, the video - * state of the call should be used to infer the camera direction. - * - * @see {@link CameraCharacteristics#LENS_FACING_FRONT} - * @see {@link CameraCharacteristics#LENS_FACING_BACK} - */ - public int getCameraDir() { - return mCameraDirection; - } - - /** - * Sets the camera direction. if camera direction is set to CAMERA_DIRECTION_UNKNOWN, the video - * state of the call should be used to infer the camera direction. - * - * @see {@link CameraCharacteristics#LENS_FACING_FRONT} - * @see {@link CameraCharacteristics#LENS_FACING_BACK} - */ - public void setCameraDir(int cameraDirection) { - if (cameraDirection == CAMERA_DIRECTION_FRONT_FACING - || cameraDirection == CAMERA_DIRECTION_BACK_FACING) { - mCameraDirection = cameraDirection; - } else { - mCameraDirection = CAMERA_DIRECTION_UNKNOWN; - } - } - - @Override - public String toString() { - return "(CameraDir:" + getCameraDir() + ")"; - } } /** @@ -1394,6 +1251,48 @@ public class DialerCall { } } + private static class VideoTechManager { + private final EmptyVideoTech emptyVideoTech = new EmptyVideoTech(); + private final VideoTech[] videoTechs; + private VideoTech savedTech; + + VideoTechManager(DialerCall call) { + String phoneNumber = call.getNumber(); + + // Insert order here determines the priority of that video tech option + videoTechs = + new VideoTech[] { + new ImsVideoTech(call, call.mTelecomCall), + new RcsVideoShare( + EnrichedCallComponent.get(call.mContext).getEnrichedCallManager(), + call, + phoneNumber != null ? phoneNumber : "") + }; + } + + VideoTech getVideoTech() { + if (savedTech != null) { + return savedTech; + } + + for (VideoTech tech : videoTechs) { + if (tech.isAvailable()) { + // Remember the first VideoTech that becomes available and always use it + savedTech = tech; + return savedTech; + } + } + + return emptyVideoTech; + } + + void dispatchCallStateChanged(int newState) { + for (VideoTech videoTech : videoTechs) { + videoTech.onCallStateChanged(newState); + } + } + } + /** Called when canned text responses have been loaded. */ public interface CannedTextResponsesLoadedListener { void onCannedTextResponsesLoaded(DialerCall call); diff --git a/java/com/android/incallui/call/DialerCallListener.java b/java/com/android/incallui/call/DialerCallListener.java index b426cd72e..fece103fa 100644 --- a/java/com/android/incallui/call/DialerCallListener.java +++ b/java/com/android/incallui/call/DialerCallListener.java @@ -16,8 +16,6 @@ package com.android.incallui.call; -import com.android.incallui.call.DialerCall.SessionModificationState; - /** Used to monitor state changes in a dialer call. */ public interface DialerCallListener { @@ -31,7 +29,7 @@ public interface DialerCallListener { void onDialerCallUpgradeToVideo(); - void onDialerCallSessionModificationStateChange(@SessionModificationState int state); + void onDialerCallSessionModificationStateChange(); void onWiFiToLteHandover(); diff --git a/java/com/android/incallui/call/InCallVideoCallCallback.java b/java/com/android/incallui/call/InCallVideoCallCallback.java deleted file mode 100644 index f897ac9dd..000000000 --- a/java/com/android/incallui/call/InCallVideoCallCallback.java +++ /dev/null @@ -1,197 +0,0 @@ -/* - * Copyright (C) 2014 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.incallui.call; - -import android.os.Handler; -import android.support.annotation.Nullable; -import android.telecom.Connection; -import android.telecom.Connection.VideoProvider; -import android.telecom.InCallService.VideoCall; -import android.telecom.VideoProfile; -import android.telecom.VideoProfile.CameraCapabilities; -import com.android.dialer.common.LogUtil; -import com.android.incallui.call.DialerCall.SessionModificationState; - -/** Implements the InCallUI VideoCall Callback. */ -public class InCallVideoCallCallback extends VideoCall.Callback implements Runnable { - - private static final int CLEAR_FAILED_REQUEST_TIMEOUT_MILLIS = 4000; - - private final DialerCall call; - @Nullable private Handler handler; - @SessionModificationState private int newSessionModificationState; - - public InCallVideoCallCallback(DialerCall call) { - this.call = call; - } - - @Override - public void onSessionModifyRequestReceived(VideoProfile videoProfile) { - LogUtil.i( - "InCallVideoCallCallback.onSessionModifyRequestReceived", "videoProfile: " + videoProfile); - int previousVideoState = VideoUtils.getUnPausedVideoState(call.getVideoState()); - int newVideoState = VideoUtils.getUnPausedVideoState(videoProfile.getVideoState()); - - boolean wasVideoCall = VideoUtils.isVideoCall(previousVideoState); - boolean isVideoCall = VideoUtils.isVideoCall(newVideoState); - - if (wasVideoCall && !isVideoCall) { - LogUtil.v( - "InCallVideoCallCallback.onSessionModifyRequestReceived", - "call downgraded to " + newVideoState); - } else if (previousVideoState != newVideoState) { - InCallVideoCallCallbackNotifier.getInstance().upgradeToVideoRequest(call, newVideoState); - } - } - - /** - * @param status Status of the session modify request. Valid values are {@link - * Connection.VideoProvider#SESSION_MODIFY_REQUEST_SUCCESS}, {@link - * Connection.VideoProvider#SESSION_MODIFY_REQUEST_FAIL}, {@link - * Connection.VideoProvider#SESSION_MODIFY_REQUEST_INVALID} - * @param responseProfile The actual profile changes made by the peer device. - */ - @Override - public void onSessionModifyResponseReceived( - int status, VideoProfile requestedProfile, VideoProfile responseProfile) { - LogUtil.i( - "InCallVideoCallCallback.onSessionModifyResponseReceived", - "status: %d, " - + "requestedProfile: %s, responseProfile: %s, current session modification state: %d", - status, - requestedProfile, - responseProfile, - call.getSessionModificationState()); - - if (call.getSessionModificationState() - == DialerCall.SESSION_MODIFICATION_STATE_WAITING_FOR_UPGRADE_TO_VIDEO_RESPONSE) { - if (handler == null) { - handler = new Handler(); - } else { - handler.removeCallbacks(this); - } - - newSessionModificationState = getDialerSessionModifyStateTelecomStatus(status); - if (status != VideoProvider.SESSION_MODIFY_REQUEST_SUCCESS) { - // This will update the video UI to display the error message. - call.setSessionModificationState(newSessionModificationState); - } - - // Wait for 4 seconds and then clean the session modification state. This allows the video UI - // to stay up so that the user can read the error message. - // - // If the other person accepted the upgrade request then this will keep the video UI up until - // the call's video state change. Without this we would switch to the voice call and then - // switch back to video UI. - handler.postDelayed(this, CLEAR_FAILED_REQUEST_TIMEOUT_MILLIS); - } else if (call.getSessionModificationState() - == DialerCall.SESSION_MODIFICATION_STATE_RECEIVED_UPGRADE_TO_VIDEO_REQUEST) { - call.setSessionModificationState(DialerCall.SESSION_MODIFICATION_STATE_NO_REQUEST); - } else if (call.getSessionModificationState() - == DialerCall.SESSION_MODIFICATION_STATE_WAITING_FOR_RESPONSE) { - call.setSessionModificationState(getDialerSessionModifyStateTelecomStatus(status)); - } else { - LogUtil.i( - "InCallVideoCallCallback.onSessionModifyResponseReceived", - "call is not waiting for " + "response, doing nothing"); - } - } - - @SessionModificationState - private int getDialerSessionModifyStateTelecomStatus(int telecomStatus) { - switch (telecomStatus) { - case VideoProvider.SESSION_MODIFY_REQUEST_SUCCESS: - return DialerCall.SESSION_MODIFICATION_STATE_NO_REQUEST; - case VideoProvider.SESSION_MODIFY_REQUEST_FAIL: - case VideoProvider.SESSION_MODIFY_REQUEST_INVALID: - // Check if it's already video call, which means the request is not video upgrade request. - if (VideoUtils.isVideoCall(call.getVideoState())) { - return DialerCall.SESSION_MODIFICATION_STATE_REQUEST_FAILED; - } else { - return DialerCall.SESSION_MODIFICATION_STATE_UPGRADE_TO_VIDEO_REQUEST_FAILED; - } - case VideoProvider.SESSION_MODIFY_REQUEST_TIMED_OUT: - return DialerCall.SESSION_MODIFICATION_STATE_UPGRADE_TO_VIDEO_REQUEST_TIMED_OUT; - case VideoProvider.SESSION_MODIFY_REQUEST_REJECTED_BY_REMOTE: - return DialerCall.SESSION_MODIFICATION_STATE_REQUEST_REJECTED; - default: - LogUtil.e( - "InCallVideoCallCallback.getDialerSessionModifyStateTelecomStatus", - "unknown status: %d", - telecomStatus); - return DialerCall.SESSION_MODIFICATION_STATE_REQUEST_FAILED; - } - } - - @Override - public void onCallSessionEvent(int event) { - InCallVideoCallCallbackNotifier.getInstance().callSessionEvent(event); - } - - @Override - public void onPeerDimensionsChanged(int width, int height) { - InCallVideoCallCallbackNotifier.getInstance().peerDimensionsChanged(call, width, height); - } - - @Override - public void onVideoQualityChanged(int videoQuality) { - InCallVideoCallCallbackNotifier.getInstance().videoQualityChanged(call, videoQuality); - } - - /** - * Handles a change to the call data usage. No implementation as the in-call UI does not display - * data usage. - * - * @param dataUsage The updated data usage. - */ - @Override - public void onCallDataUsageChanged(long dataUsage) { - LogUtil.v("InCallVideoCallCallback.onCallDataUsageChanged", "dataUsage = " + dataUsage); - InCallVideoCallCallbackNotifier.getInstance().callDataUsageChanged(dataUsage); - } - - /** - * Handles changes to the camera capabilities. No implementation as the in-call UI does not make - * use of camera capabilities. - * - * @param cameraCapabilities The changed camera capabilities. - */ - @Override - public void onCameraCapabilitiesChanged(CameraCapabilities cameraCapabilities) { - if (cameraCapabilities != null) { - InCallVideoCallCallbackNotifier.getInstance() - .cameraDimensionsChanged( - call, cameraCapabilities.getWidth(), cameraCapabilities.getHeight()); - } - } - - /** - * Called 4 seconds after the remote user responds to the video upgrade request. We use this to - * clear the session modify state. - */ - @Override - public void run() { - if (call.getSessionModificationState() == newSessionModificationState) { - LogUtil.i("InCallVideoCallCallback.onSessionModifyResponseReceived", "clearing state"); - call.setSessionModificationState(DialerCall.SESSION_MODIFICATION_STATE_NO_REQUEST); - } else { - LogUtil.i( - "InCallVideoCallCallback.onSessionModifyResponseReceived", - "session modification state has changed, not clearing state"); - } - } -} diff --git a/java/com/android/incallui/call/InCallVideoCallCallbackNotifier.java b/java/com/android/incallui/call/InCallVideoCallCallbackNotifier.java index 4a949263c..1cb9f742e 100644 --- a/java/com/android/incallui/call/InCallVideoCallCallbackNotifier.java +++ b/java/com/android/incallui/call/InCallVideoCallCallbackNotifier.java @@ -18,16 +18,12 @@ package com.android.incallui.call; import android.support.annotation.NonNull; import android.support.annotation.Nullable; -import com.android.dialer.common.LogUtil; import java.util.Collections; import java.util.Objects; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; -/** - * Class used by {@link InCallService.VideoCallCallback} to notify interested parties of incoming - * events. - */ +/** Class used to notify interested parties of incoming video related events. */ public class InCallVideoCallCallbackNotifier { /** Singleton instance of this class. */ @@ -37,12 +33,6 @@ public class InCallVideoCallCallbackNotifier { * ConcurrentHashMap constructor params: 8 is initial table size, 0.9f is load factor before * resizing, 1 means we only expect a single thread to access the map so make only a single shard */ - private final Set mSessionModificationListeners = - Collections.newSetFromMap( - new ConcurrentHashMap(8, 0.9f, 1)); - - private final Set mVideoEventListeners = - Collections.newSetFromMap(new ConcurrentHashMap(8, 0.9f, 1)); private final Set mSurfaceChangeListeners = Collections.newSetFromMap(new ConcurrentHashMap(8, 0.9f, 1)); @@ -54,48 +44,6 @@ public class InCallVideoCallCallbackNotifier { return sInstance; } - /** - * Adds a new {@link SessionModificationListener}. - * - * @param listener The listener. - */ - public void addSessionModificationListener(@NonNull SessionModificationListener listener) { - Objects.requireNonNull(listener); - mSessionModificationListeners.add(listener); - } - - /** - * Remove a {@link SessionModificationListener}. - * - * @param listener The listener. - */ - public void removeSessionModificationListener(@Nullable SessionModificationListener listener) { - if (listener != null) { - mSessionModificationListeners.remove(listener); - } - } - - /** - * Adds a new {@link VideoEventListener}. - * - * @param listener The listener. - */ - public void addVideoEventListener(@NonNull VideoEventListener listener) { - Objects.requireNonNull(listener); - mVideoEventListeners.add(listener); - } - - /** - * Remove a {@link VideoEventListener}. - * - * @param listener The listener. - */ - public void removeVideoEventListener(@Nullable VideoEventListener listener) { - if (listener != null) { - mVideoEventListeners.remove(listener); - } - } - /** * Adds a new {@link SurfaceChangeListener}. * @@ -117,56 +65,6 @@ public class InCallVideoCallCallbackNotifier { } } - /** - * Inform listeners of an upgrade to video request for a call. - * - * @param call The call. - * @param videoState The video state we want to upgrade to. - */ - public void upgradeToVideoRequest(DialerCall call, int videoState) { - LogUtil.v( - "InCallVideoCallCallbackNotifier.upgradeToVideoRequest", - "call = " + call + " new video state = " + videoState); - for (SessionModificationListener listener : mSessionModificationListeners) { - listener.onUpgradeToVideoRequest(call, videoState); - } - } - - /** - * Inform listeners of a call session event. - * - * @param event The call session event. - */ - public void callSessionEvent(int event) { - for (VideoEventListener listener : mVideoEventListeners) { - listener.onCallSessionEvent(event); - } - } - - /** - * Inform listeners of a downgrade to audio. - * - * @param call The call. - * @param paused The paused state. - */ - public void peerPausedStateChanged(DialerCall call, boolean paused) { - for (VideoEventListener listener : mVideoEventListeners) { - listener.onPeerPauseStateChanged(call, paused); - } - } - - /** - * Inform listeners of any change in the video quality of the call - * - * @param call The call. - * @param videoQuality The updated video quality of the call. - */ - public void videoQualityChanged(DialerCall call, int videoQuality) { - for (VideoEventListener listener : mVideoEventListeners) { - listener.onVideoQualityChanged(call, videoQuality); - } - } - /** * Inform listeners of a change to peer dimensions. * @@ -193,67 +91,6 @@ public class InCallVideoCallCallbackNotifier { } } - /** - * Inform listeners of a change to call data usage. - * - * @param dataUsage data usage value - */ - public void callDataUsageChanged(long dataUsage) { - for (VideoEventListener listener : mVideoEventListeners) { - listener.onCallDataUsageChange(dataUsage); - } - } - - /** Listener interface for any class that wants to be notified of upgrade to video request. */ - public interface SessionModificationListener { - - /** - * Called when a peer request is received to upgrade an audio-only call to a video call. - * - * @param call The call the request was received for. - * @param videoState The requested video state. - */ - void onUpgradeToVideoRequest(DialerCall call, int videoState); - } - - /** - * Listener interface for any class that wants to be notified of video events, including pause and - * un-pause of peer video, video quality changes. - */ - public interface VideoEventListener { - - /** - * Called when the peer pauses or un-pauses video transmission. - * - * @param call The call which paused or un-paused video transmission. - * @param paused {@code True} when the video transmission is paused, {@code false} otherwise. - */ - void onPeerPauseStateChanged(DialerCall call, boolean paused); - - /** - * Called when the video quality changes. - * - * @param call The call whose video quality changes. - * @param videoCallQuality - values are QUALITY_HIGH, MEDIUM, LOW and UNKNOWN. - */ - void onVideoQualityChanged(DialerCall call, int videoCallQuality); - - /* - * Called when call data usage value is requested or when call data usage value is updated - * because of a call state change - * - * @param dataUsage call data usage value - */ - void onCallDataUsageChange(long dataUsage); - - /** - * Called when call session event is raised. - * - * @param event The call session event. - */ - void onCallSessionEvent(int event); - } - /** * Listener interface for any class that wants to be notified of changes to the video surfaces. */ diff --git a/java/com/android/incallui/call/VideoUtils.java b/java/com/android/incallui/call/VideoUtils.java index 80fbfb1cc..b99b73222 100644 --- a/java/com/android/incallui/call/VideoUtils.java +++ b/java/com/android/incallui/call/VideoUtils.java @@ -19,113 +19,24 @@ package com.android.incallui.call; import android.content.Context; import android.content.pm.PackageManager; import android.support.annotation.NonNull; -import android.support.annotation.Nullable; import android.support.v4.content.ContextCompat; -import android.telecom.VideoProfile; -import com.android.dialer.compat.CompatUtils; import com.android.dialer.util.DialerUtils; -import com.android.incallui.call.DialerCall.SessionModificationState; -import java.util.Objects; +import com.android.incallui.videotech.VideoTech; +import com.android.incallui.videotech.VideoTech.SessionModificationState; public class VideoUtils { private static final String PREFERENCE_CAMERA_ALLOWED_BY_USER = "camera_allowed_by_user"; - public static boolean isVideoCall(@Nullable DialerCall call) { - return call != null && isVideoCall(call.getVideoState()); - } - - public static boolean isVideoCall(int videoState) { - return CompatUtils.isVideoCompatible() - && (VideoProfile.isTransmissionEnabled(videoState) - || VideoProfile.isReceptionEnabled(videoState)); - } - - public static boolean hasSentVideoUpgradeRequest(@Nullable DialerCall call) { - return call != null && hasSentVideoUpgradeRequest(call.getSessionModificationState()); - } - public static boolean hasSentVideoUpgradeRequest(@SessionModificationState int state) { - return state == DialerCall.SESSION_MODIFICATION_STATE_WAITING_FOR_UPGRADE_TO_VIDEO_RESPONSE - || state == DialerCall.SESSION_MODIFICATION_STATE_UPGRADE_TO_VIDEO_REQUEST_FAILED - || state == DialerCall.SESSION_MODIFICATION_STATE_REQUEST_REJECTED - || state == DialerCall.SESSION_MODIFICATION_STATE_UPGRADE_TO_VIDEO_REQUEST_TIMED_OUT; - } - - public static boolean hasReceivedVideoUpgradeRequest(@Nullable DialerCall call) { - return call != null && hasReceivedVideoUpgradeRequest(call.getSessionModificationState()); + return state == VideoTech.SESSION_MODIFICATION_STATE_WAITING_FOR_UPGRADE_TO_VIDEO_RESPONSE + || state == VideoTech.SESSION_MODIFICATION_STATE_UPGRADE_TO_VIDEO_REQUEST_FAILED + || state == VideoTech.SESSION_MODIFICATION_STATE_REQUEST_REJECTED + || state == VideoTech.SESSION_MODIFICATION_STATE_UPGRADE_TO_VIDEO_REQUEST_TIMED_OUT; } public static boolean hasReceivedVideoUpgradeRequest(@SessionModificationState int state) { - return state == DialerCall.SESSION_MODIFICATION_STATE_RECEIVED_UPGRADE_TO_VIDEO_REQUEST; - } - - public static boolean isBidirectionalVideoCall(DialerCall call) { - return CompatUtils.isVideoCompatible() && VideoProfile.isBidirectional(call.getVideoState()); - } - - public static boolean isTransmissionEnabled(DialerCall call) { - if (!CompatUtils.isVideoCompatible()) { - return false; - } - - return VideoProfile.isTransmissionEnabled(call.getVideoState()); - } - - public static boolean isIncomingVideoCall(DialerCall call) { - if (!VideoUtils.isVideoCall(call)) { - return false; - } - final int state = call.getState(); - return (state == DialerCall.State.INCOMING) || (state == DialerCall.State.CALL_WAITING); - } - - public static boolean isActiveVideoCall(DialerCall call) { - return VideoUtils.isVideoCall(call) && call.getState() == DialerCall.State.ACTIVE; - } - - public static boolean isOutgoingVideoCall(DialerCall call) { - if (!VideoUtils.isVideoCall(call)) { - return false; - } - final int state = call.getState(); - return DialerCall.State.isDialing(state) - || state == DialerCall.State.CONNECTING - || state == DialerCall.State.SELECT_PHONE_ACCOUNT; - } - - public static boolean isAudioCall(DialerCall call) { - if (!CompatUtils.isVideoCompatible()) { - return true; - } - - return call != null && VideoProfile.isAudioOnly(call.getVideoState()); - } - - // TODO (ims-vt) Check if special handling is needed for CONF calls. - public static boolean canVideoPause(DialerCall call) { - return isVideoCall(call) && call.getState() == DialerCall.State.ACTIVE; - } - - public static VideoProfile makeVideoPauseProfile(@NonNull DialerCall call) { - Objects.requireNonNull(call); - if (VideoProfile.isAudioOnly(call.getVideoState())) { - throw new IllegalStateException(); - } - return new VideoProfile(getPausedVideoState(call.getVideoState())); - } - - public static VideoProfile makeVideoUnPauseProfile(@NonNull DialerCall call) { - Objects.requireNonNull(call); - return new VideoProfile(getUnPausedVideoState(call.getVideoState())); - } - - public static int getUnPausedVideoState(int videoState) { - return videoState & (~VideoProfile.STATE_PAUSED); - } - - public static int getPausedVideoState(int videoState) { - return videoState | VideoProfile.STATE_PAUSED; + return state == VideoTech.SESSION_MODIFICATION_STATE_RECEIVED_UPGRADE_TO_VIDEO_REQUEST; } public static boolean hasCameraPermissionAndAllowedByUser(@NonNull Context context) { diff --git a/java/com/android/incallui/calllocation/CallLocation.java b/java/com/android/incallui/calllocation/CallLocation.java new file mode 100644 index 000000000..15a6a8e49 --- /dev/null +++ b/java/com/android/incallui/calllocation/CallLocation.java @@ -0,0 +1,32 @@ +/* + * 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.incallui.calllocation; + +import android.content.Context; +import android.support.annotation.NonNull; +import android.support.v4.app.Fragment; + +/** Used to show the user's location during an emergency call. */ +public interface CallLocation { + + boolean canGetLocation(@NonNull Context context); + + @NonNull + Fragment getLocationFragment(@NonNull Context context); + + void close(); +} diff --git a/java/com/android/incallui/calllocation/CallLocationComponent.java b/java/com/android/incallui/calllocation/CallLocationComponent.java new file mode 100644 index 000000000..6b1faf299 --- /dev/null +++ b/java/com/android/incallui/calllocation/CallLocationComponent.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.incallui.calllocation; + +import android.content.Context; +import dagger.Subcomponent; +import com.android.incallui.calllocation.stub.StubCallLocationModule; + +/** Subcomponent that can be used to access the call location implementation. */ +public class CallLocationComponent { + private static CallLocationComponent instance; + private CallLocation callLocation; + + public CallLocation getCallLocation(){ + if (callLocation == null) { + callLocation = new StubCallLocationModule.StubCallLocation(); + } + return callLocation; + } + + public static CallLocationComponent get(Context context) { + if (instance == null) { + instance = new CallLocationComponent(); + } + return instance; + } + + /** Used to refer to the root application component. */ + public interface HasComponent { + CallLocationComponent callLocationComponent(); + } +} diff --git a/java/com/android/incallui/calllocation/impl/AndroidManifest.xml b/java/com/android/incallui/calllocation/impl/AndroidManifest.xml new file mode 100644 index 000000000..550c5808c --- /dev/null +++ b/java/com/android/incallui/calllocation/impl/AndroidManifest.xml @@ -0,0 +1,26 @@ + + + + + + + + diff --git a/java/com/android/incallui/calllocation/impl/AuthException.java b/java/com/android/incallui/calllocation/impl/AuthException.java new file mode 100644 index 000000000..26def2fc9 --- /dev/null +++ b/java/com/android/incallui/calllocation/impl/AuthException.java @@ -0,0 +1,25 @@ +/* + * 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.incallui.calllocation.impl; + +/** For detecting backend authorization errors */ +public class AuthException extends Exception { + + public AuthException(String detailMessage) { + super(detailMessage); + } +} diff --git a/java/com/android/incallui/calllocation/impl/CallLocationImpl.java b/java/com/android/incallui/calllocation/impl/CallLocationImpl.java new file mode 100644 index 000000000..20f5ffb0f --- /dev/null +++ b/java/com/android/incallui/calllocation/impl/CallLocationImpl.java @@ -0,0 +1,67 @@ +/* + * 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.incallui.calllocation.impl; + +import android.content.Context; +import android.support.annotation.MainThread; +import android.support.annotation.NonNull; +import android.support.v4.app.Fragment; +import com.android.dialer.common.Assert; +import com.android.incallui.calllocation.CallLocation; +import javax.inject.Inject; + +/** Uses Google Play Services to show the user's location during an emergency call. */ +public class CallLocationImpl implements CallLocation { + + private LocationHelper locationHelper; + private LocationFragment locationFragment; + + @Inject + public CallLocationImpl() {} + + @MainThread + @Override + public boolean canGetLocation(@NonNull Context context) { + Assert.isMainThread(); + return LocationHelper.canGetLocation(context); + } + + @MainThread + @NonNull + @Override + public Fragment getLocationFragment(@NonNull Context context) { + Assert.isMainThread(); + if (locationFragment == null) { + locationFragment = new LocationFragment(); + locationHelper = new LocationHelper(context); + locationHelper.addLocationListener(locationFragment.getPresenter()); + } + return locationFragment; + } + + @MainThread + @Override + public void close() { + Assert.isMainThread(); + if (locationFragment != null) { + locationHelper.removeLocationListener(locationFragment.getPresenter()); + locationHelper.close(); + locationFragment = null; + locationHelper = null; + } + } +} diff --git a/java/com/android/incallui/calllocation/impl/CallLocationModule.java b/java/com/android/incallui/calllocation/impl/CallLocationModule.java new file mode 100644 index 000000000..73e85554e --- /dev/null +++ b/java/com/android/incallui/calllocation/impl/CallLocationModule.java @@ -0,0 +1,29 @@ +/* + * 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.incallui.calllocation.impl; + +import com.android.incallui.calllocation.CallLocation; +import dagger.Binds; +import dagger.Module; + +/** This module provides an instance of call location. */ +@Module +public abstract class CallLocationModule { + + @Binds + public abstract CallLocation bindCallLocation(CallLocationImpl callLocation); +} diff --git a/java/com/android/incallui/calllocation/impl/DownloadMapImageTask.java b/java/com/android/incallui/calllocation/impl/DownloadMapImageTask.java new file mode 100644 index 000000000..801b0d35c --- /dev/null +++ b/java/com/android/incallui/calllocation/impl/DownloadMapImageTask.java @@ -0,0 +1,77 @@ +/* + * 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.incallui.calllocation.impl; + +import android.graphics.drawable.Drawable; +import android.location.Location; +import android.net.TrafficStats; +import android.os.AsyncTask; +import com.android.dialer.common.LogUtil; +import com.android.incallui.calllocation.impl.LocationPresenter.LocationUi; +import java.io.InputStream; +import java.lang.ref.WeakReference; +import java.net.URL; + +class DownloadMapImageTask extends AsyncTask { + + private static final String STATIC_MAP_SRC_NAME = "src"; + + private final WeakReference mUiReference; + + public DownloadMapImageTask(WeakReference uiReference) { + mUiReference = uiReference; + } + + @Override + protected Drawable doInBackground(Location... locations) { + LocationUi ui = mUiReference.get(); + if (ui == null) { + return null; + } + if (locations == null || locations.length == 0) { + LogUtil.e("DownloadMapImageTask.doInBackground", "No location provided"); + return null; + } + + try { + URL mapUrl = new URL(LocationUrlBuilder.getStaticMapUrl(ui.getContext(), locations[0])); + InputStream content = (InputStream) mapUrl.getContent(); + + TrafficStats.setThreadStatsTag(TrafficStatsTags.DOWNLOAD_LOCATION_MAP_TAG); + return Drawable.createFromStream(content, STATIC_MAP_SRC_NAME); + } catch (Exception ex) { + LogUtil.e("DownloadMapImageTask.doInBackground", "Exception!!!", ex); + return null; + } finally { + TrafficStats.clearThreadStatsTag(); + } + } + + @Override + protected void onPostExecute(Drawable mapImage) { + LocationUi ui = mUiReference.get(); + if (ui == null) { + return; + } + + try { + ui.setMap(mapImage); + } catch (Exception ex) { + LogUtil.e("DownloadMapImageTask.onPostExecute", "Exception!!!", ex); + } + } +} diff --git a/java/com/android/incallui/calllocation/impl/GoogleLocationSettingHelper.java b/java/com/android/incallui/calllocation/impl/GoogleLocationSettingHelper.java new file mode 100644 index 000000000..18a80b8ce --- /dev/null +++ b/java/com/android/incallui/calllocation/impl/GoogleLocationSettingHelper.java @@ -0,0 +1,123 @@ +/* + * 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.incallui.calllocation.impl; + +import android.content.ContentResolver; +import android.content.Context; +import android.content.Intent; +import android.content.pm.PackageManager; +import android.content.pm.ResolveInfo; +import android.database.Cursor; +import android.net.Uri; +import android.provider.Settings.Secure; +import android.provider.Settings.SettingNotFoundException; +import com.android.dialer.common.LogUtil; + +/** + * Helper class to check if Google Location Services is enabled. This class is based on + * https://docs.google.com/a/google.com/document/d/1sGm8pHgGY1QmxbLCwTZuWQASEDN7CFW9EPSZXAuGQfo + */ +public class GoogleLocationSettingHelper { + + /** User has disagreed to use location for Google services. */ + public static final int USE_LOCATION_FOR_SERVICES_OFF = 0; + /** User has agreed to use location for Google services. */ + public static final int USE_LOCATION_FOR_SERVICES_ON = 1; + /** The user has neither agreed nor disagreed to use location for Google services yet. */ + public static final int USE_LOCATION_FOR_SERVICES_NOT_SET = 2; + + private static final String GOOGLE_SETTINGS_AUTHORITY = "com.google.settings"; + private static final Uri GOOGLE_SETTINGS_CONTENT_URI = + Uri.parse("content://" + GOOGLE_SETTINGS_AUTHORITY + "/partner"); + private static final String NAME = "name"; + private static final String VALUE = "value"; + private static final String USE_LOCATION_FOR_SERVICES = "use_location_for_services"; + + /** Determine if Google apps need to conform to the USE_LOCATION_FOR_SERVICES setting. */ + public static boolean isEnforceable(Context context) { + final ResolveInfo ri = + context + .getPackageManager() + .resolveActivity( + new Intent("com.google.android.gsf.GOOGLE_APPS_LOCATION_SETTINGS"), + PackageManager.MATCH_DEFAULT_ONLY); + return ri != null; + } + + /** + * Get the current value for the 'Use value for location' setting. + * + * @return One of {@link #USE_LOCATION_FOR_SERVICES_NOT_SET}, {@link + * #USE_LOCATION_FOR_SERVICES_OFF} or {@link #USE_LOCATION_FOR_SERVICES_ON}. + */ + private static int getUseLocationForServices(Context context) { + final ContentResolver resolver = context.getContentResolver(); + Cursor c = null; + String stringValue = null; + try { + c = + resolver.query( + GOOGLE_SETTINGS_CONTENT_URI, + new String[] {VALUE}, + NAME + "=?", + new String[] {USE_LOCATION_FOR_SERVICES}, + null); + if (c != null && c.moveToNext()) { + stringValue = c.getString(0); + } + } catch (final RuntimeException e) { + LogUtil.e( + "GoogleLocationSettingHelper.getUseLocationForServices", + "Failed to get 'Use My Location' setting", + e); + } finally { + if (c != null) { + c.close(); + } + } + if (stringValue == null) { + return USE_LOCATION_FOR_SERVICES_NOT_SET; + } + int value; + try { + value = Integer.parseInt(stringValue); + } catch (final NumberFormatException nfe) { + value = USE_LOCATION_FOR_SERVICES_NOT_SET; + } + return value; + } + + /** Whether or not the system location setting is enable */ + public static boolean isSystemLocationSettingEnabled(Context context) { + try { + return Secure.getInt(context.getContentResolver(), Secure.LOCATION_MODE) + != Secure.LOCATION_MODE_OFF; + } catch (SettingNotFoundException e) { + LogUtil.e( + "GoogleLocationSettingHelper.isSystemLocationSettingEnabled", + "Failed to get System Location setting", + e); + return false; + } + } + + /** Convenience method that returns true is GLS is ON or if it's not enforceable. */ + public static boolean isGoogleLocationServicesEnabled(Context context) { + return !isEnforceable(context) + || getUseLocationForServices(context) == USE_LOCATION_FOR_SERVICES_ON; + } +} diff --git a/java/com/android/incallui/calllocation/impl/HttpFetcher.java b/java/com/android/incallui/calllocation/impl/HttpFetcher.java new file mode 100644 index 000000000..7bfbaa6ef --- /dev/null +++ b/java/com/android/incallui/calllocation/impl/HttpFetcher.java @@ -0,0 +1,289 @@ +/* + * 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.incallui.calllocation.impl; + +import static com.android.dialer.util.DialerUtils.closeQuietly; + +import android.content.Context; +import android.net.Uri; +import android.net.Uri.Builder; +import android.os.SystemClock; +import android.util.Pair; +import com.android.dialer.common.LogUtil; +import com.android.dialer.util.MoreStrings; +import com.google.android.common.http.UrlRules; +import java.io.ByteArrayOutputStream; +import java.io.FilterInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.ProtocolException; +import java.net.URL; +import java.util.List; +import java.util.Objects; +import java.util.Set; + +/** Utility for making http requests. */ +public class HttpFetcher { + + // Phone number + public static final String PARAM_ID = "id"; + // auth token + public static final String PARAM_ACCESS_TOKEN = "access_token"; + private static final String TAG = HttpFetcher.class.getSimpleName(); + + /** + * Send a http request to the given url. + * + * @param urlString The url to request. + * @return The response body as a byte array. Or {@literal null} if status code is not 2xx. + * @throws java.io.IOException when an error occurs. + */ + public static byte[] sendRequestAsByteArray( + Context context, String urlString, String requestMethod, List> headers) + throws IOException, AuthException { + Objects.requireNonNull(urlString); + + URL url = reWriteUrl(context, urlString); + if (url == null) { + return null; + } + + HttpURLConnection conn = null; + InputStream is = null; + boolean isError = false; + final long start = SystemClock.uptimeMillis(); + try { + conn = (HttpURLConnection) url.openConnection(); + setMethodAndHeaders(conn, requestMethod, headers); + int responseCode = conn.getResponseCode(); + LogUtil.i("HttpFetcher.sendRequestAsByteArray", "response code: " + responseCode); + // All 2xx codes are successful. + if (responseCode / 100 == 2) { + is = conn.getInputStream(); + } else { + is = conn.getErrorStream(); + isError = true; + } + + final ByteArrayOutputStream baos = new ByteArrayOutputStream(); + final byte[] buffer = new byte[1024]; + int bytesRead; + + while ((bytesRead = is.read(buffer)) != -1) { + baos.write(buffer, 0, bytesRead); + } + + if (isError) { + handleBadResponse(url.toString(), baos.toByteArray()); + if (responseCode == 401) { + throw new AuthException("Auth error"); + } + return null; + } + + byte[] response = baos.toByteArray(); + LogUtil.i("HttpFetcher.sendRequestAsByteArray", "received " + response.length + " bytes"); + long end = SystemClock.uptimeMillis(); + LogUtil.i("HttpFetcher.sendRequestAsByteArray", "fetch took " + (end - start) + " ms"); + return response; + } finally { + closeQuietly(is); + if (conn != null) { + conn.disconnect(); + } + } + } + + /** + * Send a http request to the given url. + * + * @return The response body as a InputStream. Or {@literal null} if status code is not 2xx. + * @throws java.io.IOException when an error occurs. + */ + public static InputStream sendRequestAsInputStream( + Context context, String urlString, String requestMethod, List> headers) + throws IOException, AuthException { + Objects.requireNonNull(urlString); + + URL url = reWriteUrl(context, urlString); + if (url == null) { + return null; + } + + HttpURLConnection httpUrlConnection = null; + boolean isSuccess = false; + try { + httpUrlConnection = (HttpURLConnection) url.openConnection(); + setMethodAndHeaders(httpUrlConnection, requestMethod, headers); + int responseCode = httpUrlConnection.getResponseCode(); + LogUtil.i("HttpFetcher.sendRequestAsInputStream", "response code: " + responseCode); + + if (responseCode == 401) { + throw new AuthException("Auth error"); + } else if (responseCode / 100 == 2) { // All 2xx codes are successful. + InputStream is = httpUrlConnection.getInputStream(); + if (is != null) { + is = new HttpInputStreamWrapper(httpUrlConnection, is); + isSuccess = true; + return is; + } + } + + return null; + } finally { + if (httpUrlConnection != null && !isSuccess) { + httpUrlConnection.disconnect(); + } + } + } + + /** + * Set http method and headers. + * + * @param conn The connection to add headers to. + * @param requestMethod request method + * @param headers http headers where the first item in the pair is the key and second item is the + * value. + */ + private static void setMethodAndHeaders( + HttpURLConnection conn, String requestMethod, List> headers) + throws ProtocolException { + conn.setRequestMethod(requestMethod); + if (headers != null) { + for (Pair pair : headers) { + conn.setRequestProperty(pair.first, pair.second); + } + } + } + + private static String obfuscateUrl(String urlString) { + final Uri uri = Uri.parse(urlString); + final Builder builder = + new Builder().scheme(uri.getScheme()).authority(uri.getAuthority()).path(uri.getPath()); + final Set names = uri.getQueryParameterNames(); + for (String name : names) { + if (PARAM_ACCESS_TOKEN.equals(name)) { + builder.appendQueryParameter(name, "token"); + } else { + final String value = uri.getQueryParameter(name); + if (PARAM_ID.equals(name)) { + builder.appendQueryParameter(name, MoreStrings.toSafeString(value)); + } else { + builder.appendQueryParameter(name, value); + } + } + } + return builder.toString(); + } + + /** Same as {@link #getRequestAsString(Context, String, String, List)} with null headers. */ + public static String getRequestAsString(Context context, String urlString) + throws IOException, AuthException { + return getRequestAsString(context, urlString, "GET" /* Default to get. */, null); + } + + /** + * Send a http request to the given url. + * + * @param context The android context. + * @param urlString The url to request. + * @param headers Http headers to pass in the request. {@literal null} is allowed. + * @return The response body as a String. Or {@literal null} if status code is not 2xx. + * @throws java.io.IOException when an error occurs. + */ + public static String getRequestAsString( + Context context, String urlString, String requestMethod, List> headers) + throws IOException, AuthException { + final byte[] byteArr = sendRequestAsByteArray(context, urlString, requestMethod, headers); + if (byteArr == null) { + // Encountered error response... just return. + return null; + } + final String response = new String(byteArr); + LogUtil.i("HttpFetcher.getRequestAsString", "response body: " + response); + return response; + } + + /** + * Lookup up url re-write rules from gServices and apply to the given url. + * + *

https://wiki.corp.google.com/twiki/bin/view/Main/AndroidGservices#URL_Rewriting_Rules + * + * @return The new url. + */ + private static URL reWriteUrl(Context context, String url) { + final UrlRules rules = UrlRules.getRules(context.getContentResolver()); + final UrlRules.Rule rule = rules.matchRule(url); + final String newUrl = rule.apply(url); + + if (newUrl == null) { + if (LogUtil.isDebugEnabled()) { + // Url is blocked by re-write. + LogUtil.i( + "HttpFetcher.reWriteUrl", + "url " + obfuscateUrl(url) + " is blocked. Ignoring request."); + } + return null; + } + + if (LogUtil.isDebugEnabled()) { + LogUtil.i("HttpFetcher.reWriteUrl", "fetching " + obfuscateUrl(newUrl)); + if (!newUrl.equals(url)) { + LogUtil.i( + "HttpFetcher.reWriteUrl", + "Original url: " + obfuscateUrl(url) + ", after re-write: " + obfuscateUrl(newUrl)); + } + } + + URL urlObject = null; + try { + urlObject = new URL(newUrl); + } catch (MalformedURLException e) { + LogUtil.e("HttpFetcher.reWriteUrl", "failed to parse url: " + url, e); + } + return urlObject; + } + + private static void handleBadResponse(String url, byte[] response) { + LogUtil.i("HttpFetcher.handleBadResponse", "Got bad response code from url: " + url); + LogUtil.i("HttpFetcher.handleBadResponse", new String(response)); + } + + /** Disconnect {@link HttpURLConnection} when InputStream is closed */ + private static class HttpInputStreamWrapper extends FilterInputStream { + + final HttpURLConnection mHttpUrlConnection; + final long mStartMillis = SystemClock.uptimeMillis(); + + public HttpInputStreamWrapper(HttpURLConnection conn, InputStream in) { + super(in); + mHttpUrlConnection = conn; + } + + @Override + public void close() throws IOException { + super.close(); + mHttpUrlConnection.disconnect(); + if (LogUtil.isDebugEnabled()) { + long endMillis = SystemClock.uptimeMillis(); + LogUtil.i("HttpFetcher.close", "fetch took " + (endMillis - mStartMillis) + " ms"); + } + } + } +} diff --git a/java/com/android/incallui/calllocation/impl/LocationFragment.java b/java/com/android/incallui/calllocation/impl/LocationFragment.java new file mode 100644 index 000000000..b152cd683 --- /dev/null +++ b/java/com/android/incallui/calllocation/impl/LocationFragment.java @@ -0,0 +1,197 @@ +/* + * 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.incallui.calllocation.impl; + +import android.animation.LayoutTransition; +import android.content.Context; +import android.graphics.drawable.Drawable; +import android.location.Location; +import android.os.Bundle; +import android.os.Handler; +import android.text.TextUtils; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.TextView; +import android.widget.ViewAnimator; +import com.android.dialer.common.LogUtil; +import com.android.incallui.baseui.BaseFragment; +import java.util.Objects; +import java.util.concurrent.TimeUnit; + +/** + * Fragment which shows location during E911 calls, to supplement the user with accurate location + * information in case the user is asked for their location by the emergency responder. + * + *

If location data is inaccurate, stale, or unavailable, this should not be shown. + */ +public class LocationFragment extends BaseFragment + implements LocationPresenter.LocationUi { + + private static final String ADDRESS_DELIMITER = ","; + + // Indexes used to animate fading between views + private static final int LOADING_VIEW_INDEX = 0; + private static final int LOCATION_VIEW_INDEX = 1; + private static final long TIMEOUT_MILLIS = TimeUnit.SECONDS.toMillis(5); + + private ViewAnimator viewAnimator; + private ImageView locationMap; + private TextView addressLine1; + private TextView addressLine2; + private TextView latLongLine; + private Location location; + private ViewGroup locationLayout; + + private boolean isMapSet; + private boolean isAddressSet; + private boolean isLocationSet; + private boolean hasTimeoutStarted; + + private final Handler handler = new Handler(); + private final Runnable dataTimeoutRunnable = + () -> { + LogUtil.i( + "LocationFragment.dataTimeoutRunnable", + "timed out so animate any future layout changes"); + locationLayout.setLayoutTransition(new LayoutTransition()); + showLocationNow(); + }; + + @Override + public LocationPresenter createPresenter() { + return new LocationPresenter(); + } + + @Override + public LocationPresenter.LocationUi getUi() { + return this; + } + + @Override + public View onCreateView( + LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + final View view = inflater.inflate(R.layout.location_fragment, container, false); + viewAnimator = (ViewAnimator) view.findViewById(R.id.location_view_animator); + locationMap = (ImageView) view.findViewById(R.id.location_map); + addressLine1 = (TextView) view.findViewById(R.id.address_line_one); + addressLine2 = (TextView) view.findViewById(R.id.address_line_two); + latLongLine = (TextView) view.findViewById(R.id.lat_long_line); + locationLayout = (ViewGroup) view.findViewById(R.id.location_layout); + view.setOnClickListener( + v -> { + LogUtil.enterBlock("LocationFragment.onCreateView"); + launchMap(); + }); + return view; + } + + @Override + public void onDestroy() { + super.onDestroy(); + handler.removeCallbacks(dataTimeoutRunnable); + } + + @Override + public void setMap(Drawable mapImage) { + LogUtil.enterBlock("LocationFragment.setMap"); + isMapSet = true; + locationMap.setVisibility(View.VISIBLE); + locationMap.setImageDrawable(mapImage); + displayWhenReady(); + } + + @Override + public void setAddress(String address) { + LogUtil.i("LocationFragment.setAddress", address); + isAddressSet = true; + addressLine1.setVisibility(View.VISIBLE); + addressLine2.setVisibility(View.VISIBLE); + if (TextUtils.isEmpty(address)) { + addressLine1.setText(null); + addressLine2.setText(null); + } else { + + // Split the address after the first delimiter for display, if present. + // For example, "1600 Amphitheatre Parkway, Mountain View, CA 94043" + // => "1600 Amphitheatre Parkway" + // => "Mountain View, CA 94043" + int splitIndex = address.indexOf(ADDRESS_DELIMITER); + if (splitIndex >= 0) { + updateText(addressLine1, address.substring(0, splitIndex).trim()); + updateText(addressLine2, address.substring(splitIndex + 1).trim()); + } else { + updateText(addressLine1, address); + updateText(addressLine2, null); + } + } + displayWhenReady(); + } + + @Override + public void setLocation(Location location) { + LogUtil.i("LocationFragment.setLocation", String.valueOf(location)); + isLocationSet = true; + this.location = location; + + if (location != null) { + latLongLine.setVisibility(View.VISIBLE); + latLongLine.setText( + getContext() + .getString( + R.string.lat_long_format, location.getLatitude(), location.getLongitude())); + } + displayWhenReady(); + } + + private void displayWhenReady() { + // Show the location if all data has loaded, otherwise prime the timeout + if (isMapSet && isAddressSet && isLocationSet) { + showLocationNow(); + } else if (!hasTimeoutStarted) { + handler.postDelayed(dataTimeoutRunnable, TIMEOUT_MILLIS); + hasTimeoutStarted = true; + } + } + + private void showLocationNow() { + handler.removeCallbacks(dataTimeoutRunnable); + if (viewAnimator.getDisplayedChild() != LOCATION_VIEW_INDEX) { + viewAnimator.setDisplayedChild(LOCATION_VIEW_INDEX); + } + } + + @Override + public Context getContext() { + return getActivity(); + } + + private void launchMap() { + if (location != null) { + startActivity( + LocationUrlBuilder.getShowMapIntent( + location, addressLine1.getText(), addressLine2.getText())); + } + } + + private static void updateText(TextView view, String text) { + if (!Objects.equals(text, view.getText())) { + view.setText(text); + } + } +} diff --git a/java/com/android/incallui/calllocation/impl/LocationHelper.java b/java/com/android/incallui/calllocation/impl/LocationHelper.java new file mode 100644 index 000000000..645e9b86a --- /dev/null +++ b/java/com/android/incallui/calllocation/impl/LocationHelper.java @@ -0,0 +1,219 @@ +/* + * 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.incallui.calllocation.impl; + +import android.content.Context; +import android.location.Location; +import android.net.ConnectivityManager; +import android.net.NetworkInfo; +import android.os.Bundle; +import android.os.Handler; +import android.support.annotation.MainThread; +import com.android.dialer.common.Assert; +import com.android.dialer.common.LogUtil; +import com.android.dialer.util.PermissionsUtil; +import com.google.android.gms.common.ConnectionResult; +import com.google.android.gms.common.api.GoogleApiClient; +import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks; +import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener; +import com.google.android.gms.common.api.ResultCallback; +import com.google.android.gms.common.api.Status; +import com.google.android.gms.location.LocationListener; +import com.google.android.gms.location.LocationRequest; +import com.google.android.gms.location.LocationServices; +import java.util.ArrayList; +import java.util.List; + +/** Uses the Fused location service to get location and pass updates on to listeners. */ +public class LocationHelper { + + private static final int MIN_UPDATE_INTERVAL_MS = 30 * 1000; + private static final int LAST_UPDATE_THRESHOLD_MS = 60 * 1000; + private static final int LOCATION_ACCURACY_THRESHOLD_METERS = 100; + + private final LocationHelperInternal locationHelperInternal; + private final List listeners = new ArrayList<>(); + + @MainThread + LocationHelper(Context context) { + Assert.isMainThread(); + Assert.checkArgument(canGetLocation(context)); + locationHelperInternal = new LocationHelperInternal(context); + } + + static boolean canGetLocation(Context context) { + if (!PermissionsUtil.hasLocationPermissions(context)) { + LogUtil.i("LocationHelper.canGetLocation", "no location permissions."); + return false; + } + + // Ensure that both system location setting is on and google location services are enabled. + if (!GoogleLocationSettingHelper.isGoogleLocationServicesEnabled(context) + || !GoogleLocationSettingHelper.isSystemLocationSettingEnabled(context)) { + LogUtil.i("LocationHelper.canGetLocation", "location service is disabled."); + return false; + } + return true; + } + + /** + * Whether the location is valid. We consider it valid if it was recorded within the specified + * time threshold of the present and has an accuracy less than the specified distance threshold. + * + * @param location The location to determine the validity of. + * @return {@code true} if the location is valid, and {@code false} otherwise. + */ + static boolean isValidLocation(Location location) { + if (location != null) { + long locationTimeMs = location.getTime(); + long elapsedTimeMs = System.currentTimeMillis() - locationTimeMs; + if (elapsedTimeMs > LAST_UPDATE_THRESHOLD_MS) { + LogUtil.i("LocationHelper.isValidLocation", "stale location, age: " + elapsedTimeMs); + return false; + } + if (location.getAccuracy() > LOCATION_ACCURACY_THRESHOLD_METERS) { + LogUtil.i("LocationHelper.isValidLocation", "poor accuracy: " + location.getAccuracy()); + return false; + } + return true; + } + LogUtil.i("LocationHelper.isValidLocation", "no location"); + return false; + } + + @MainThread + void addLocationListener(LocationListener listener) { + Assert.isMainThread(); + listeners.add(listener); + } + + @MainThread + void removeLocationListener(LocationListener listener) { + Assert.isMainThread(); + listeners.remove(listener); + } + + @MainThread + void close() { + Assert.isMainThread(); + LogUtil.enterBlock("LocationHelper.close"); + listeners.clear(); + + if (locationHelperInternal != null) { + locationHelperInternal.close(); + } + } + + @MainThread + void onLocationChanged(Location location, boolean isConnected) { + Assert.isMainThread(); + LogUtil.i("LocationHelper.onLocationChanged", "location: " + location); + + for (LocationListener listener : listeners) { + listener.onLocationChanged(location); + } + } + + /** + * This class contains all the asynchronous callbacks. It only posts location changes back to the + * outer class on the main thread. + */ + private class LocationHelperInternal + implements ConnectionCallbacks, OnConnectionFailedListener, LocationListener { + + private final GoogleApiClient apiClient; + private final ConnectivityManager connectivityManager; + private final Handler mainThreadHandler = new Handler(); + + @MainThread + LocationHelperInternal(Context context) { + Assert.isMainThread(); + apiClient = + new GoogleApiClient.Builder(context) + .addApi(LocationServices.API) + .addConnectionCallbacks(this) + .addOnConnectionFailedListener(this) + .build(); + + LogUtil.i("LocationHelperInternal", "Connecting to location service..."); + apiClient.connect(); + + connectivityManager = + (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); + } + + void close() { + if (apiClient.isConnected()) { + LogUtil.i("LocationHelperInternal", "disconnecting"); + LocationServices.FusedLocationApi.removeLocationUpdates(apiClient, this); + apiClient.disconnect(); + } + } + + @Override + public void onConnected(Bundle bundle) { + LogUtil.enterBlock("LocationHelperInternal.onConnected"); + LocationRequest locationRequest = + LocationRequest.create() + .setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY) + .setInterval(MIN_UPDATE_INTERVAL_MS) + .setFastestInterval(MIN_UPDATE_INTERVAL_MS); + + LocationServices.FusedLocationApi.requestLocationUpdates(apiClient, locationRequest, this) + .setResultCallback( + new ResultCallback() { + @Override + public void onResult(Status status) { + if (status.getStatus().isSuccess()) { + onLocationChanged(LocationServices.FusedLocationApi.getLastLocation(apiClient)); + } + } + }); + } + + @Override + public void onConnectionSuspended(int i) { + // Do nothing. + } + + @Override + public void onConnectionFailed(ConnectionResult result) { + // Do nothing. + } + + @Override + public void onLocationChanged(Location location) { + // Post new location on main thread + mainThreadHandler.post( + new Runnable() { + @Override + public void run() { + LocationHelper.this.onLocationChanged(location, isConnected()); + } + }); + } + + /** @return Whether the phone is connected to data. */ + private boolean isConnected() { + if (connectivityManager == null) { + return false; + } + NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); + return networkInfo != null && networkInfo.isConnectedOrConnecting(); + } + } +} diff --git a/java/com/android/incallui/calllocation/impl/LocationPresenter.java b/java/com/android/incallui/calllocation/impl/LocationPresenter.java new file mode 100644 index 000000000..a56fd3b3c --- /dev/null +++ b/java/com/android/incallui/calllocation/impl/LocationPresenter.java @@ -0,0 +1,98 @@ +/* + * 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.incallui.calllocation.impl; + +import android.content.Context; +import android.graphics.drawable.Drawable; +import android.location.Location; +import android.os.AsyncTask; +import com.android.dialer.common.LogUtil; +import com.android.incallui.baseui.Presenter; +import com.android.incallui.baseui.Ui; +import com.google.android.gms.location.LocationListener; +import java.lang.ref.WeakReference; +import java.util.Objects; + +/** + * Presenter for the {@code LocationFragment}. + * + *

Performs lookup for the address and map image to show. + */ +public class LocationPresenter extends Presenter + implements LocationListener { + + private Location mLastLocation; + private AsyncTask mDownloadMapTask; + private AsyncTask mReverseGeocodeTask; + + LocationPresenter() {} + + @Override + public void onUiReady(LocationUi ui) { + LogUtil.i("LocationPresenter.onUiReady", ""); + super.onUiReady(ui); + updateLocation(mLastLocation, true); + } + + @Override + public void onUiUnready(LocationUi ui) { + LogUtil.i("LocationPresenter.onUiUnready", ""); + super.onUiUnready(ui); + + if (mDownloadMapTask != null) { + mDownloadMapTask.cancel(true); + } + if (mReverseGeocodeTask != null) { + mReverseGeocodeTask.cancel(true); + } + } + + @Override + public void onLocationChanged(Location location) { + LogUtil.i("LocationPresenter.onLocationChanged", ""); + updateLocation(location, false); + } + + private void updateLocation(Location location, boolean forceUpdate) { + LogUtil.i("LocationPresenter.updateLocation", "location: " + location); + if (forceUpdate || !Objects.equals(mLastLocation, location)) { + mLastLocation = location; + if (LocationHelper.isValidLocation(location)) { + LocationUi ui = getUi(); + mDownloadMapTask = new DownloadMapImageTask(new WeakReference<>(ui)).execute(location); + mReverseGeocodeTask = new ReverseGeocodeTask(new WeakReference<>(ui)).execute(location); + if (ui != null) { + ui.setLocation(location); + } else { + LogUtil.i("LocationPresenter.updateLocation", "no Ui"); + } + } + } + } + + /** UI interface */ + public interface LocationUi extends Ui { + + void setAddress(String address); + + void setMap(Drawable mapImage); + + void setLocation(Location location); + + Context getContext(); + } +} diff --git a/java/com/android/incallui/calllocation/impl/LocationUrlBuilder.java b/java/com/android/incallui/calllocation/impl/LocationUrlBuilder.java new file mode 100644 index 000000000..a57bdf613 --- /dev/null +++ b/java/com/android/incallui/calllocation/impl/LocationUrlBuilder.java @@ -0,0 +1,177 @@ +/* + * 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.incallui.calllocation.impl; + +import android.content.Context; +import android.content.Intent; +import android.content.res.Resources; +import android.location.Location; +import android.net.Uri; +import android.support.annotation.Nullable; +import android.support.annotation.VisibleForTesting; +import java.util.Locale; + +class LocationUrlBuilder { + + // Static Map API path constants. + private static final String HTTPS_SCHEME = "https"; + private static final String MAPS_API_DOMAIN = "maps.googleapis.com"; + private static final String MAPS_PATH = "maps"; + private static final String API_PATH = "api"; + private static final String STATIC_MAP_PATH = "staticmap"; + private static final String GEOCODE_PATH = "geocode"; + private static final String GEOCODE_OUTPUT_TYPE = "json"; + + // Static Map API parameter constants. + private static final String KEY_PARAM_KEY = "key"; + private static final String CENTER_PARAM_KEY = "center"; + private static final String ZOOM_PARAM_KEY = "zoom"; + private static final String SCALE_PARAM_KEY = "scale"; + private static final String SIZE_PARAM_KEY = "size"; + private static final String MARKERS_PARAM_KEY = "markers"; + + private static final String ZOOM_PARAM_VALUE = Integer.toString(16); + + private static final String LAT_LONG_DELIMITER = ","; + + private static final String MARKER_DELIMITER = "|"; + private static final String MARKER_STYLE_DELIMITER = ":"; + private static final String MARKER_STYLE_COLOR = "color"; + private static final String MARKER_STYLE_COLOR_RED = "red"; + + private static final String LAT_LNG_PARAM_KEY = "latlng"; + + private static final String ANDROID_API_KEY_VALUE = "AIzaSyAXdDnif6B7sBYxU8hzw9qAp3pRPVHs060"; + private static final String BROWSER_API_KEY_VALUE = "AIzaSyBfLlvWYndiQ3RFEHli65qGQH36QIxdyCI"; + + /** + * Generates the URL to a static map image for the given location. + * + *

This image has the following characteristics: + * + *

- It is centered at the given latitude and longitutde. - It is scaled according to the + * device's pixel density. - There is a red marker at the given latitude and longitude. + * + *

Source: https://developers.google.com/maps/documentation/staticmaps/ + * + * @param contxt The context. + * @param Location A location. + * @return The URL of a static map image url of the given location. + */ + public static String getStaticMapUrl(Context context, Location location) { + final Uri.Builder builder = new Uri.Builder(); + Resources res = context.getResources(); + String size = + res.getDimensionPixelSize(R.dimen.location_map_width) + + "x" + + res.getDimensionPixelSize(R.dimen.location_map_height); + + builder + .scheme(HTTPS_SCHEME) + .authority(MAPS_API_DOMAIN) + .appendPath(MAPS_PATH) + .appendPath(API_PATH) + .appendPath(STATIC_MAP_PATH) + .appendQueryParameter(CENTER_PARAM_KEY, getFormattedLatLng(location)) + .appendQueryParameter(ZOOM_PARAM_KEY, ZOOM_PARAM_VALUE) + .appendQueryParameter(SIZE_PARAM_KEY, size) + .appendQueryParameter(SCALE_PARAM_KEY, Float.toString(res.getDisplayMetrics().density)) + .appendQueryParameter(MARKERS_PARAM_KEY, getMarkerUrlParamValue(location)) + .appendQueryParameter(KEY_PARAM_KEY, ANDROID_API_KEY_VALUE); + + return builder.build().toString(); + } + + /** + * Generates the URL for a request to reverse geocode the given location. + * + *

Source: https://developers.google.com/maps/documentation/geocoding/#ReverseGeocoding + * + * @param Location A location. + */ + public static String getReverseGeocodeUrl(Location location) { + final Uri.Builder builder = new Uri.Builder(); + + builder + .scheme(HTTPS_SCHEME) + .authority(MAPS_API_DOMAIN) + .appendPath(MAPS_PATH) + .appendPath(API_PATH) + .appendPath(GEOCODE_PATH) + .appendPath(GEOCODE_OUTPUT_TYPE) + .appendQueryParameter(LAT_LNG_PARAM_KEY, getFormattedLatLng(location)) + .appendQueryParameter(KEY_PARAM_KEY, BROWSER_API_KEY_VALUE); + + return builder.build().toString(); + } + + public static Intent getShowMapIntent( + Location location, @Nullable CharSequence addressLine1, @Nullable CharSequence addressLine2) { + + String latLong = getFormattedLatLng(location); + String url = String.format(Locale.US, "geo: %s?q=%s", latLong, latLong); + + // Add a map label + if (addressLine1 != null) { + if (addressLine2 != null) { + url += + String.format(Locale.US, "(%s, %s)", addressLine1.toString(), addressLine2.toString()); + } else { + url += String.format(Locale.US, "(%s)", addressLine1.toString()); + } + } else { + // TODO: i18n + url += + String.format( + Locale.US, + "(Latitude: %f, Longitude: %f)", + location.getLatitude(), + location.getLongitude()); + } + + Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); + intent.setPackage("com.google.android.apps.maps"); + return intent; + } + + /** + * Returns a comma-separated latitude and longitude pair, formatted for use as a URL parameter + * value. + * + * @param location A location. + * @return The comma-separated latitude and longitude pair of that location. + */ + @VisibleForTesting + static String getFormattedLatLng(Location location) { + return location.getLatitude() + LAT_LONG_DELIMITER + location.getLongitude(); + } + + /** + * Returns the URL parameter value for the marker, specifying its style and position. + * + * @param location A location. + * @return The URL parameter value for the marker. + */ + @VisibleForTesting + static String getMarkerUrlParamValue(Location location) { + return MARKER_STYLE_COLOR + + MARKER_STYLE_DELIMITER + + MARKER_STYLE_COLOR_RED + + MARKER_DELIMITER + + getFormattedLatLng(location); + } +} diff --git a/java/com/android/incallui/calllocation/impl/ReverseGeocodeTask.java b/java/com/android/incallui/calllocation/impl/ReverseGeocodeTask.java new file mode 100644 index 000000000..eb5957b05 --- /dev/null +++ b/java/com/android/incallui/calllocation/impl/ReverseGeocodeTask.java @@ -0,0 +1,144 @@ +/* + * 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.incallui.calllocation.impl; + +import android.location.Location; +import android.net.TrafficStats; +import android.os.AsyncTask; +import com.android.dialer.common.LogUtil; +import com.android.incallui.calllocation.impl.LocationPresenter.LocationUi; +import java.lang.ref.WeakReference; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +class ReverseGeocodeTask extends AsyncTask { + + // Below are the JSON keys for the reverse geocode response. + // Source: https://developers.google.com/maps/documentation/geocoding/#ReverseGeocoding + private static final String JSON_KEY_RESULTS = "results"; + private static final String JSON_KEY_ADDRESS = "formatted_address"; + private static final String JSON_KEY_ADDRESS_COMPONENTS = "address_components"; + private static final String JSON_KEY_PREMISE = "premise"; + private static final String JSON_KEY_TYPES = "types"; + private static final String JSON_KEY_LONG_NAME = "long_name"; + private static final String JSON_KEY_SHORT_NAME = "short_name"; + + private WeakReference mUiReference; + + public ReverseGeocodeTask(WeakReference uiReference) { + mUiReference = uiReference; + } + + @Override + protected String doInBackground(Location... locations) { + LocationUi ui = mUiReference.get(); + if (ui == null) { + return null; + } + if (locations == null || locations.length == 0) { + LogUtil.e("ReverseGeocodeTask.onLocationChanged", "No location provided"); + return null; + } + + try { + String address = null; + String url = LocationUrlBuilder.getReverseGeocodeUrl(locations[0]); + + TrafficStats.setThreadStatsTag(TrafficStatsTags.REVERSE_GEOCODE_TAG); + String jsonResponse = HttpFetcher.getRequestAsString(ui.getContext(), url); + + // Parse the JSON response for the formatted address of the first result. + JSONObject responseObject = new JSONObject(jsonResponse); + if (responseObject != null) { + JSONArray results = responseObject.optJSONArray(JSON_KEY_RESULTS); + if (results != null && results.length() > 0) { + JSONObject topResult = results.optJSONObject(0); + if (topResult != null) { + address = topResult.getString(JSON_KEY_ADDRESS); + + // Strip off the Premise component from the address, if present. + JSONArray components = topResult.optJSONArray(JSON_KEY_ADDRESS_COMPONENTS); + if (components != null) { + boolean stripped = false; + for (int i = 0; !stripped && i < components.length(); i++) { + JSONObject component = components.optJSONObject(i); + JSONArray types = component.optJSONArray(JSON_KEY_TYPES); + if (types != null) { + for (int j = 0; !stripped && j < types.length(); j++) { + if (JSON_KEY_PREMISE.equals(types.getString(j))) { + String premise = null; + if (component.has(JSON_KEY_SHORT_NAME) + && address.startsWith(component.getString(JSON_KEY_SHORT_NAME))) { + premise = component.getString(JSON_KEY_SHORT_NAME); + } else if (component.has(JSON_KEY_LONG_NAME) + && address.startsWith(component.getString(JSON_KEY_LONG_NAME))) { + premise = component.getString(JSON_KEY_SHORT_NAME); + } + if (premise != null) { + int index = address.indexOf(',', premise.length()); + if (index > 0 && index < address.length()) { + address = address.substring(index + 1).trim(); + } + stripped = true; + break; + } + } + } + } + } + } + + // Strip off the country, if its USA. Note: unfortunately the country in the formatted + // address field doesn't match the country in the address component fields (USA != US) + // so we can't easily strip off the country for all cases, thus this hack. + if (address.endsWith(", USA")) { + address = address.substring(0, address.length() - 5); + } + } + } + } + + return address; + } catch (AuthException ex) { + LogUtil.e("ReverseGeocodeTask.onLocationChanged", "AuthException", ex); + return null; + } catch (JSONException ex) { + LogUtil.e("ReverseGeocodeTask.onLocationChanged", "JSONException", ex); + return null; + } catch (Exception ex) { + LogUtil.e("ReverseGeocodeTask.onLocationChanged", "Exception!!!", ex); + return null; + } finally { + TrafficStats.clearThreadStatsTag(); + } + } + + @Override + protected void onPostExecute(String address) { + LocationUi ui = mUiReference.get(); + if (ui == null) { + return; + } + + try { + ui.setAddress(address); + } catch (Exception ex) { + LogUtil.e("ReverseGeocodeTask.onPostExecute", "Exception!!!", ex); + } + } +} diff --git a/java/com/android/incallui/calllocation/impl/TrafficStatsTags.java b/java/com/android/incallui/calllocation/impl/TrafficStatsTags.java new file mode 100644 index 000000000..02cc2e083 --- /dev/null +++ b/java/com/android/incallui/calllocation/impl/TrafficStatsTags.java @@ -0,0 +1,29 @@ +/* + * 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.incallui.calllocation.impl; + +/** Constants used for logging */ +public class TrafficStatsTags { + + /** + * Must be greater than {@link com.android.contacts.common.util.TrafficStatsTags#TAG_MAX}, to + * respect the namespace of the tags in ContactsCommon. + */ + public static final int DOWNLOAD_LOCATION_MAP_TAG = 0xd000; + + public static final int REVERSE_GEOCODE_TAG = 0xd001; +} diff --git a/java/com/android/incallui/calllocation/impl/res/layout/location_fragment.xml b/java/com/android/incallui/calllocation/impl/res/layout/location_fragment.xml new file mode 100644 index 000000000..a6bd07542 --- /dev/null +++ b/java/com/android/incallui/calllocation/impl/res/layout/location_fragment.xml @@ -0,0 +1,134 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/java/com/android/incallui/calllocation/impl/res/values/dimens.xml b/java/com/android/incallui/calllocation/impl/res/values/dimens.xml new file mode 100644 index 000000000..1f4181607 --- /dev/null +++ b/java/com/android/incallui/calllocation/impl/res/values/dimens.xml @@ -0,0 +1,6 @@ + + + + 92dp + 92dp + diff --git a/java/com/android/incallui/calllocation/impl/res/values/strings.xml b/java/com/android/incallui/calllocation/impl/res/values/strings.xml new file mode 100644 index 000000000..ef7c1624c --- /dev/null +++ b/java/com/android/incallui/calllocation/impl/res/values/strings.xml @@ -0,0 +1,15 @@ + + + + + Emergency Location Map + + + You are here + + %f, %f + + + Finding your location + + diff --git a/java/com/android/incallui/calllocation/impl/res/values/styles.xml b/java/com/android/incallui/calllocation/impl/res/values/styles.xml new file mode 100644 index 000000000..866a4edb6 --- /dev/null +++ b/java/com/android/incallui/calllocation/impl/res/values/styles.xml @@ -0,0 +1,28 @@ + + + + + + + + + + + + diff --git a/java/com/android/incallui/calllocation/stub/StubCallLocationModule.java b/java/com/android/incallui/calllocation/stub/StubCallLocationModule.java new file mode 100644 index 000000000..fc198c724 --- /dev/null +++ b/java/com/android/incallui/calllocation/stub/StubCallLocationModule.java @@ -0,0 +1,54 @@ +/* + * 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.incallui.calllocation.stub; + +import android.content.Context; +import android.support.annotation.NonNull; +import android.support.v4.app.Fragment; +import com.android.dialer.common.Assert; +import com.android.incallui.calllocation.CallLocation; +import dagger.Binds; +import dagger.Module; +import javax.inject.Inject; + +/** This module provides an instance of call location. */ +@Module +public abstract class StubCallLocationModule { + + @Binds + public abstract CallLocation bindCallLocation(StubCallLocation callLocation); + + static public class StubCallLocation implements CallLocation { + @Inject + public StubCallLocation() {} + + @Override + public boolean canGetLocation(@NonNull Context context) { + return false; + } + + @Override + @NonNull + public Fragment getLocationFragment(@NonNull Context context) { + return null; + } + + @Override + public void close() { + } + } +} diff --git a/java/com/android/incallui/commontheme/res/anim/blinking.xml b/java/com/android/incallui/commontheme/res/anim/blinking.xml new file mode 100644 index 000000000..aaec18c56 --- /dev/null +++ b/java/com/android/incallui/commontheme/res/anim/blinking.xml @@ -0,0 +1,10 @@ + + + + \ No newline at end of file diff --git a/java/com/android/incallui/contactgrid/BottomRow.java b/java/com/android/incallui/contactgrid/BottomRow.java index aaf7e8214..6ddce4533 100644 --- a/java/com/android/incallui/contactgrid/BottomRow.java +++ b/java/com/android/incallui/contactgrid/BottomRow.java @@ -31,10 +31,10 @@ import com.android.incallui.incall.protocol.PrimaryInfo; * Gets the content of the bottom row. For example: * *

    - *
  • Mobile +1 (650) 253-0000 - *
  • [HD icon] 00:15 - *
  • Call ended - *
  • Hanging up + *
  • Mobile +1 (650) 253-0000 + *
  • [HD attempting icon]/[HD icon] 00:15 + *
  • Call ended + *
  • Hanging up *
*/ public class BottomRow { @@ -45,6 +45,7 @@ public class BottomRow { @Nullable public final CharSequence label; public final boolean isTimerVisible; public final boolean isWorkIconVisible; + public final boolean isHdAttemptinIconVisible; public final boolean isHdIconVisible; public final boolean isForwardIconVisible; public final boolean isSpamIconVisible; @@ -54,6 +55,7 @@ public class BottomRow { @Nullable CharSequence label, boolean isTimerVisible, boolean isWorkIconVisible, + boolean isHdAttemptinIconVisible, boolean isHdIconVisible, boolean isForwardIconVisible, boolean isSpamIconVisible, @@ -61,6 +63,7 @@ public class BottomRow { this.label = label; this.isTimerVisible = isTimerVisible; this.isWorkIconVisible = isWorkIconVisible; + this.isHdAttemptinIconVisible = isHdAttemptinIconVisible; this.isHdIconVisible = isHdIconVisible; this.isForwardIconVisible = isForwardIconVisible; this.isSpamIconVisible = isSpamIconVisible; @@ -76,6 +79,7 @@ public class BottomRow { boolean isForwardIconVisible = state.isForwardedNumber; boolean isWorkIconVisible = state.isWorkCall; boolean isHdIconVisible = state.isHdAudioCall && !isForwardIconVisible; + boolean isHdAttemptingIconVisible = state.isHdAttempting; boolean isSpamIconVisible = false; boolean shouldPopulateAccessibilityEvent = true; @@ -110,6 +114,7 @@ public class BottomRow { label, isTimerVisible, isWorkIconVisible, + isHdAttemptingIconVisible, isHdIconVisible, isForwardIconVisible, isSpamIconVisible, diff --git a/java/com/android/incallui/contactgrid/ContactGridManager.java b/java/com/android/incallui/contactgrid/ContactGridManager.java index 81c225163..a0b687c2d 100644 --- a/java/com/android/incallui/contactgrid/ContactGridManager.java +++ b/java/com/android/incallui/contactgrid/ContactGridManager.java @@ -18,10 +18,14 @@ package com.android.incallui.contactgrid; import android.content.Context; import android.os.SystemClock; +import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.telecom.TelecomManager; import android.text.TextUtils; import android.view.View; import android.view.accessibility.AccessibilityEvent; +import android.view.animation.Animation; +import android.view.animation.AnimationUtils; import android.widget.Chronometer; import android.widget.ImageView; import android.widget.TextView; @@ -56,7 +60,7 @@ public class ContactGridManager { @Nullable private ImageView avatarImageView; // Row 2: Mobile +1 (650) 253-0000 - // Row 2: [HD icon] 00:15 + // Row 2: [HD attempting icon]/[HD icon] 00:15 // Row 2: Call ended // Row 2: Hanging up // Row 2: [Alert sign] Suspected spam caller @@ -77,7 +81,6 @@ public class ContactGridManager { private PrimaryCallState primaryCallState = PrimaryCallState.createEmptyPrimaryCallState(); private final LetterTileDrawable letterTile; - public ContactGridManager( View view, @Nullable ImageView avatarImageView, int avatarSize, boolean showAnonymousAvatar) { context = view.getContext(); @@ -226,6 +229,24 @@ public class ContactGridManager { } } + /** + * Returns the appropriate LetterTileDrawable.TYPE_ based on a given call state. + * + *

If no special state is detected, yields TYPE_DEFAULT. + */ + private static @LetterTileDrawable.ContactType int getContactTypeForPrimaryCallState( + @NonNull PrimaryCallState callState, @NonNull PrimaryInfo primaryInfo) { + if (callState.isVoiceMailNumber) { + return LetterTileDrawable.TYPE_VOICEMAIL; + } else if (callState.isBusinessNumber) { + return LetterTileDrawable.TYPE_BUSINESS; + } else if (primaryInfo.numberPresentation == TelecomManager.PRESENTATION_RESTRICTED) { + return LetterTileDrawable.TYPE_GENERIC_AVATAR; + } else { + return LetterTileDrawable.TYPE_DEFAULT; + } + } + /** * Updates row 1. For example: * @@ -255,7 +276,7 @@ public class ContactGridManager { if (avatarImageView != null) { if (hideAvatar) { avatarImageView.setVisibility(View.GONE); - } else if (avatarImageView != null && avatarSize > 0 && updateAvatarVisibility()) { + } else if (avatarSize > 0 && updateAvatarVisibility()) { boolean hasPhoto = primaryInfo.photo != null && primaryInfo.photoType == ContactPhotoType.CONTACT; // Contact has a photo, don't render a letter tile. @@ -265,27 +286,29 @@ public class ContactGridManager { context, primaryInfo.photo, avatarSize, avatarSize)); // Contact has a name, that isn't a number. } else { - int contactType = - primaryCallState.isVoiceMailNumber - ? LetterTileDrawable.TYPE_VOICEMAIL - : LetterTileDrawable.TYPE_DEFAULT; letterTile.setCanonicalDialerLetterTileDetails( primaryInfo.name, primaryInfo.contactInfoLookupKey, LetterTileDrawable.SHAPE_CIRCLE, - contactType); + getContactTypeForPrimaryCallState(primaryCallState, primaryInfo)); + + // By invalidating the avatarImageView we force a redraw of the letter tile. + // This is required to properly display the updated letter tile iconography based on the + // contact type, because the background drawable reference cached in the view, and the + // view is not aware of the mutations made to the background. + avatarImageView.invalidate(); avatarImageView.setBackground(letterTile); + } } } } - } /** * Updates row 2. For example: * *

    *
  • Mobile +1 (650) 253-0000 - *
  • [HD icon] 00:15 + *
  • [HD attempting icon]/[HD icon] 00:15 *
  • Call ended *
  • Hanging up *
@@ -296,7 +319,15 @@ public class ContactGridManager { bottomTextView.setText(info.label); bottomTextView.setAllCaps(info.isSpamIconVisible); workIconImageView.setVisibility(info.isWorkIconVisible ? View.VISIBLE : View.GONE); - hdIconImageView.setVisibility(info.isHdIconVisible ? View.VISIBLE : View.GONE); + boolean wasHdIconVisible = hdIconImageView.getVisibility() == View.VISIBLE; + if (!wasHdIconVisible && info.isHdAttemptinIconVisible) { + Animation animation = AnimationUtils.loadAnimation(context, R.anim.blinking); + hdIconImageView.startAnimation(animation); + } else if (wasHdIconVisible && !info.isHdAttemptinIconVisible) { + hdIconImageView.clearAnimation(); + } + hdIconImageView.setVisibility( + info.isHdIconVisible || info.isHdAttemptinIconVisible ? View.VISIBLE : View.GONE); forwardIconImageView.setVisibility(info.isForwardIconVisible ? View.VISIBLE : View.GONE); spamIconImageView.setVisibility(info.isSpamIconVisible ? View.VISIBLE : View.GONE); diff --git a/java/com/android/incallui/contactgrid/TopRow.java b/java/com/android/incallui/contactgrid/TopRow.java index a340fd0a0..ecd5eea64 100644 --- a/java/com/android/incallui/contactgrid/TopRow.java +++ b/java/com/android/incallui/contactgrid/TopRow.java @@ -21,10 +21,10 @@ import android.graphics.drawable.Drawable; import android.support.annotation.Nullable; import android.text.TextUtils; import com.android.dialer.common.Assert; -import com.android.incallui.call.DialerCall; import com.android.incallui.call.DialerCall.State; import com.android.incallui.call.VideoUtils; import com.android.incallui.incall.protocol.PrimaryCallState; +import com.android.incallui.videotech.VideoTech; /** * Gets the content of the top row. For example: @@ -95,7 +95,7 @@ public class TopRow { } private static CharSequence getLabelForIncoming(Context context, PrimaryCallState state) { - if (VideoUtils.isVideoCall(state.videoState)) { + if (state.isVideoCall) { return getLabelForIncomingVideo(context, state.isWifi); } else if (state.isWifi && !TextUtils.isEmpty(state.connectionLabel)) { return state.connectionLabel; @@ -120,7 +120,7 @@ public class TopRow { if (!TextUtils.isEmpty(state.connectionLabel) && !state.isWifi) { return context.getString(R.string.incall_calling_via_template, state.connectionLabel); } else { - if (VideoUtils.isVideoCall(state.videoState)) { + if (state.isVideoCall) { if (state.isWifi) { return context.getString(R.string.incall_wifi_video_call_requesting); } else { @@ -144,18 +144,18 @@ public class TopRow { private static CharSequence getLabelForVideoRequest(Context context, PrimaryCallState state) { switch (state.sessionModificationState) { - case DialerCall.SESSION_MODIFICATION_STATE_WAITING_FOR_UPGRADE_TO_VIDEO_RESPONSE: + case VideoTech.SESSION_MODIFICATION_STATE_WAITING_FOR_UPGRADE_TO_VIDEO_RESPONSE: return context.getString(R.string.incall_video_call_requesting); - case DialerCall.SESSION_MODIFICATION_STATE_REQUEST_FAILED: - case DialerCall.SESSION_MODIFICATION_STATE_UPGRADE_TO_VIDEO_REQUEST_FAILED: + case VideoTech.SESSION_MODIFICATION_STATE_REQUEST_FAILED: + case VideoTech.SESSION_MODIFICATION_STATE_UPGRADE_TO_VIDEO_REQUEST_FAILED: return context.getString(R.string.incall_video_call_request_failed); - case DialerCall.SESSION_MODIFICATION_STATE_REQUEST_REJECTED: + case VideoTech.SESSION_MODIFICATION_STATE_REQUEST_REJECTED: return context.getString(R.string.incall_video_call_request_rejected); - case DialerCall.SESSION_MODIFICATION_STATE_UPGRADE_TO_VIDEO_REQUEST_TIMED_OUT: + case VideoTech.SESSION_MODIFICATION_STATE_UPGRADE_TO_VIDEO_REQUEST_TIMED_OUT: return context.getString(R.string.incall_video_call_request_timed_out); - case DialerCall.SESSION_MODIFICATION_STATE_RECEIVED_UPGRADE_TO_VIDEO_REQUEST: + case VideoTech.SESSION_MODIFICATION_STATE_RECEIVED_UPGRADE_TO_VIDEO_REQUEST: return getLabelForIncomingVideo(context, state.isWifi); - case DialerCall.SESSION_MODIFICATION_STATE_NO_REQUEST: + case VideoTech.SESSION_MODIFICATION_STATE_NO_REQUEST: default: Assert.fail(); return null; diff --git a/java/com/android/incallui/contactgrid/res/layout/incall_contactgrid_bottom_row.xml b/java/com/android/incallui/contactgrid/res/layout/incall_contactgrid_bottom_row.xml index 3900be556..b7a3fe7d4 100644 --- a/java/com/android/incallui/contactgrid/res/layout/incall_contactgrid_bottom_row.xml +++ b/java/com/android/incallui/contactgrid/res/layout/incall_contactgrid_bottom_row.xml @@ -4,8 +4,8 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:orientation="horizontal" android:gravity="center_horizontal" + android:orientation="horizontal" tools:showIn="@layout/incall_contact_grid"> diff --git a/java/com/android/incallui/incall/impl/AutoValue_MappedButtonConfig_MappingInfo.java b/java/com/android/incallui/incall/impl/AutoValue_MappedButtonConfig_MappingInfo.java deleted file mode 100644 index addebc484..000000000 --- a/java/com/android/incallui/incall/impl/AutoValue_MappedButtonConfig_MappingInfo.java +++ /dev/null @@ -1,135 +0,0 @@ -/* - * 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.incallui.incall.impl; - -import javax.annotation.Generated; - -@Generated("com.google.auto.value.processor.AutoValueProcessor") - final class AutoValue_MappedButtonConfig_MappingInfo extends MappedButtonConfig.MappingInfo { - - private final int slot; - private final int slotOrder; - private final int conflictOrder; - - private AutoValue_MappedButtonConfig_MappingInfo( - int slot, - int slotOrder, - int conflictOrder) { - this.slot = slot; - this.slotOrder = slotOrder; - this.conflictOrder = conflictOrder; - } - - @Override - public int getSlot() { - return slot; - } - - @Override - public int getSlotOrder() { - return slotOrder; - } - - @Override - public int getConflictOrder() { - return conflictOrder; - } - - @Override - public String toString() { - return "MappingInfo{" - + "slot=" + slot + ", " - + "slotOrder=" + slotOrder + ", " - + "conflictOrder=" + conflictOrder - + "}"; - } - - @Override - public boolean equals(Object o) { - if (o == this) { - return true; - } - if (o instanceof MappedButtonConfig.MappingInfo) { - MappedButtonConfig.MappingInfo that = (MappedButtonConfig.MappingInfo) o; - return (this.slot == that.getSlot()) - && (this.slotOrder == that.getSlotOrder()) - && (this.conflictOrder == that.getConflictOrder()); - } - return false; - } - - @Override - public int hashCode() { - int h = 1; - h *= 1000003; - h ^= this.slot; - h *= 1000003; - h ^= this.slotOrder; - h *= 1000003; - h ^= this.conflictOrder; - return h; - } - - static final class Builder extends MappedButtonConfig.MappingInfo.Builder { - private Integer slot; - private Integer slotOrder; - private Integer conflictOrder; - Builder() { - } - private Builder(MappedButtonConfig.MappingInfo source) { - this.slot = source.getSlot(); - this.slotOrder = source.getSlotOrder(); - this.conflictOrder = source.getConflictOrder(); - } - @Override - public MappedButtonConfig.MappingInfo.Builder setSlot(int slot) { - this.slot = slot; - return this; - } - @Override - public MappedButtonConfig.MappingInfo.Builder setSlotOrder(int slotOrder) { - this.slotOrder = slotOrder; - return this; - } - @Override - public MappedButtonConfig.MappingInfo.Builder setConflictOrder(int conflictOrder) { - this.conflictOrder = conflictOrder; - return this; - } - @Override - public MappedButtonConfig.MappingInfo build() { - String missing = ""; - if (this.slot == null) { - missing += " slot"; - } - if (this.slotOrder == null) { - missing += " slotOrder"; - } - if (this.conflictOrder == null) { - missing += " conflictOrder"; - } - if (!missing.isEmpty()) { - throw new IllegalStateException("Missing required properties:" + missing); - } - return new AutoValue_MappedButtonConfig_MappingInfo( - this.slot, - this.slotOrder, - this.conflictOrder); - } - } - -} diff --git a/java/com/android/incallui/incall/impl/FakeDragAnimation.java b/java/com/android/incallui/incall/impl/FakeDragAnimation.java new file mode 100644 index 000000000..c84c3c409 --- /dev/null +++ b/java/com/android/incallui/incall/impl/FakeDragAnimation.java @@ -0,0 +1,62 @@ +/* + * 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.incallui.incall.impl; + +import android.animation.ValueAnimator; +import android.animation.ValueAnimator.AnimatorUpdateListener; +import android.support.v4.view.ViewPager; +import android.support.v4.view.animation.FastOutSlowInInterpolator; + +/** + * An animation that controls the fake drag of a {@link ViewPager}. See {@link + * ViewPager#fakeDragBy(float)} for more details. + */ +public class FakeDragAnimation implements AnimatorUpdateListener { + + /** The view to animate. */ + private final ViewPager pager; + + private final ValueAnimator animator; + private int oldDragPosition; + + public FakeDragAnimation(ViewPager pager) { + this.pager = pager; + animator = ValueAnimator.ofInt(0, pager.getWidth()); + animator.addUpdateListener(this); + animator.setInterpolator(new FastOutSlowInInterpolator()); + animator.setDuration(600); + } + + public void start() { + animator.start(); + } + + @Override + public void onAnimationUpdate(ValueAnimator animation) { + if (!pager.isFakeDragging()) { + pager.beginFakeDrag(); + } + int dragPosition = (Integer) animation.getAnimatedValue(); + int dragOffset = dragPosition - oldDragPosition; + oldDragPosition = dragPosition; + pager.fakeDragBy(-dragOffset); + + if (animation.getAnimatedFraction() == 1) { + pager.endFakeDrag(); + } + } +} diff --git a/java/com/android/incallui/incall/impl/InCallFragment.java b/java/com/android/incallui/incall/impl/InCallFragment.java index ef8a1edd8..3f31651a0 100644 --- a/java/com/android/incallui/incall/impl/InCallFragment.java +++ b/java/com/android/incallui/incall/impl/InCallFragment.java @@ -213,9 +213,7 @@ public class InCallFragment extends Fragment @Override public void setPrimary(@NonNull PrimaryInfo primaryInfo) { LogUtil.i("InCallFragment.setPrimary", primaryInfo.toString()); - if (adapter == null) { - initAdapter(primaryInfo.multimediaData); - } + setAdapterMedia(primaryInfo.multimediaData); contactGridManager.setPrimary(primaryInfo); if (primaryInfo.shouldShowLocation) { @@ -241,9 +239,13 @@ public class InCallFragment extends Fragment } } - private void initAdapter(MultimediaData multimediaData) { - adapter = new InCallPagerAdapter(getChildFragmentManager(), multimediaData); - pager.setAdapter(adapter); + private void setAdapterMedia(MultimediaData multimediaData) { + if (adapter == null) { + adapter = new InCallPagerAdapter(getChildFragmentManager(), multimediaData); + pager.setAdapter(adapter); + } else { + adapter.setAttachments(multimediaData); + } if (adapter.getCount() > 1) { tabLayout.setVisibility(pager.getVisibility()); @@ -251,16 +253,13 @@ public class InCallFragment extends Fragment if (!stateRestored) { new Handler() .postDelayed( - new Runnable() { - @Override - public void run() { - // In order to prevent user confusion and educate the user on our UI, we animate - // the view pager to the button grid after 2 seconds show them when the UI is - // that they are more familiar with. - pager.setCurrentItem(adapter.getButtonGridPosition()); - } + () -> { + // In order to prevent user confusion and educate the user on our UI, we animate + // the view pager to the button grid after a short period to show them where the + // UI that they are more familiar with is located. + new FakeDragAnimation(pager).start(); }, - 2000); + 333); } } else { tabLayout.setVisibility(View.GONE); @@ -479,23 +478,39 @@ public class InCallFragment extends Fragment @Override public boolean isShowingLocationUi() { - Fragment fragment = getChildFragmentManager().findFragmentById(R.id.incall_location_holder); + Fragment fragment = getLocationFragment(); return fragment != null && fragment.isVisible(); } @Override public void showLocationUi(@Nullable Fragment locationUi) { - boolean isShowing = isShowingLocationUi(); - if (!isShowing && locationUi != null) { + boolean isVisible = isShowingLocationUi(); + if (locationUi != null && !isVisible) { // Show the location fragment. getChildFragmentManager() .beginTransaction() .replace(R.id.incall_location_holder, locationUi) .commitAllowingStateLoss(); - } else if (isShowing && locationUi == null) { + } else if (locationUi == null && isVisible) { // Hide the location fragment - Fragment fragment = getChildFragmentManager().findFragmentById(R.id.incall_location_holder); - getChildFragmentManager().beginTransaction().remove(fragment).commitAllowingStateLoss(); + getChildFragmentManager() + .beginTransaction() + .remove(getLocationFragment()) + .commitAllowingStateLoss(); } } + + @Override + public void onMultiWindowModeChanged(boolean isInMultiWindowMode) { + super.onMultiWindowModeChanged(isInMultiWindowMode); + if (isInMultiWindowMode == isShowingLocationUi()) { + LogUtil.i("InCallFragment.onMultiWindowModeChanged", "hide = " + isInMultiWindowMode); + // Need to show or hide location + showLocationUi(isInMultiWindowMode ? null : getLocationFragment()); + } + } + + private Fragment getLocationFragment() { + return getChildFragmentManager().findFragmentById(R.id.incall_location_holder); + } } diff --git a/java/com/android/incallui/incall/impl/InCallPagerAdapter.java b/java/com/android/incallui/incall/impl/InCallPagerAdapter.java index 50eb4c8c3..2e2183565 100644 --- a/java/com/android/incallui/incall/impl/InCallPagerAdapter.java +++ b/java/com/android/incallui/incall/impl/InCallPagerAdapter.java @@ -19,17 +19,18 @@ package com.android.incallui.incall.impl; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; -import android.support.v4.app.FragmentPagerAdapter; +import android.support.v4.app.FragmentStatePagerAdapter; +import android.support.v4.view.PagerAdapter; import android.text.TextUtils; import com.android.dialer.multimedia.MultimediaData; import com.android.incallui.sessiondata.MultimediaFragment; /** View pager adapter for in call ui. */ -public class InCallPagerAdapter extends FragmentPagerAdapter { +public class InCallPagerAdapter extends FragmentStatePagerAdapter { - @Nullable private final MultimediaData attachments; + @Nullable private MultimediaData attachments; - public InCallPagerAdapter(FragmentManager fragmentManager, MultimediaData attachments) { + public InCallPagerAdapter(FragmentManager fragmentManager, @Nullable MultimediaData attachments) { super(fragmentManager); this.attachments = attachments; } @@ -47,13 +48,27 @@ public class InCallPagerAdapter extends FragmentPagerAdapter { @Override public int getCount() { if (attachments != null - && (!TextUtils.isEmpty(attachments.getSubject()) || attachments.hasImageData())) { + && (!TextUtils.isEmpty(attachments.getText()) || attachments.hasImageData())) { return 2; } return 1; } + public void setAttachments(@Nullable MultimediaData attachments) { + if (this.attachments != attachments) { + this.attachments = attachments; + notifyDataSetChanged(); + } + } + public int getButtonGridPosition() { return getCount() - 1; } + + //this is called when notifyDataSetChanged() is called + @Override + public int getItemPosition(Object object) { + // refresh all fragments when data set changed + return PagerAdapter.POSITION_NONE; + } } diff --git a/java/com/android/incallui/incall/impl/MappedButtonConfig.java b/java/com/android/incallui/incall/impl/MappedButtonConfig.java index ecdb5dfea..722983796 100644 --- a/java/com/android/incallui/incall/impl/MappedButtonConfig.java +++ b/java/com/android/incallui/incall/impl/MappedButtonConfig.java @@ -22,7 +22,7 @@ import android.util.ArraySet; import com.android.dialer.common.Assert; import com.android.incallui.incall.protocol.InCallButtonIds; import com.android.incallui.incall.protocol.InCallButtonIdsExtension; - +import com.google.auto.value.AutoValue; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; @@ -151,7 +151,7 @@ final class MappedButtonConfig { } /** Holds information about button mapping. */ - + @AutoValue abstract static class MappingInfo { /** The Ui slot into which a given button desires to be placed. */ @@ -179,7 +179,7 @@ final class MappedButtonConfig { } /** Class used to build instances of {@link MappingInfo}. */ - + @AutoValue.Builder abstract static class Builder { public abstract Builder setSlot(int slot); diff --git a/java/com/android/incallui/incall/protocol/PrimaryCallState.java b/java/com/android/incallui/incall/protocol/PrimaryCallState.java index 782090832..6e1680b4b 100644 --- a/java/com/android/incallui/incall/protocol/PrimaryCallState.java +++ b/java/com/android/incallui/incall/protocol/PrimaryCallState.java @@ -18,15 +18,15 @@ package com.android.incallui.incall.protocol; import android.graphics.drawable.Drawable; import android.telecom.DisconnectCause; -import android.telecom.VideoProfile; import com.android.incallui.call.DialerCall; -import com.android.incallui.call.DialerCall.SessionModificationState; +import com.android.incallui.videotech.VideoTech; +import com.android.incallui.videotech.VideoTech.SessionModificationState; import java.util.Locale; /** State of the primary call. */ public class PrimaryCallState { public final int state; - public final int videoState; + public final boolean isVideoCall; @SessionModificationState public final int sessionModificationState; public final DisconnectCause disconnectCause; public final String connectionLabel; @@ -37,19 +37,21 @@ public class PrimaryCallState { public final boolean isWifi; public final boolean isConference; public final boolean isWorkCall; + public final boolean isHdAttempting; public final boolean isHdAudioCall; public final boolean isForwardedNumber; public final boolean shouldShowContactPhoto; public final long connectTimeMillis; public final boolean isVoiceMailNumber; public final boolean isRemotelyHeld; + public final boolean isBusinessNumber; // TODO: Convert to autovalue. b/34502119 public static PrimaryCallState createEmptyPrimaryCallState() { return new PrimaryCallState( DialerCall.State.IDLE, - VideoProfile.STATE_AUDIO_ONLY, - DialerCall.SESSION_MODIFICATION_STATE_NO_REQUEST, + false, /* isVideoCall */ + VideoTech.SESSION_MODIFICATION_STATE_NO_REQUEST, new DisconnectCause(DisconnectCause.UNKNOWN), null, /* connectionLabel */ null, /* connectionIcon */ @@ -59,17 +61,19 @@ public class PrimaryCallState { false /* isWifi */, false /* isConference */, false /* isWorkCall */, + false /* isHdAttempting */, false /* isHdAudioCall */, false /* isForwardedNumber */, false /* shouldShowContactPhoto */, 0, false /* isVoiceMailNumber */, - false /* isRemotelyHeld */); + false /* isRemotelyHeld */, + false /* isBusinessNumber */); } public PrimaryCallState( int state, - int videoState, + boolean isVideoCall, @SessionModificationState int sessionModificationState, DisconnectCause disconnectCause, String connectionLabel, @@ -80,14 +84,16 @@ public class PrimaryCallState { boolean isWifi, boolean isConference, boolean isWorkCall, + boolean isHdAttempting, boolean isHdAudioCall, boolean isForwardedNumber, boolean shouldShowContactPhoto, long connectTimeMillis, boolean isVoiceMailNumber, - boolean isRemotelyHeld) { + boolean isRemotelyHeld, + boolean isBusinessNumber) { this.state = state; - this.videoState = videoState; + this.isVideoCall = isVideoCall; this.sessionModificationState = sessionModificationState; this.disconnectCause = disconnectCause; this.connectionLabel = connectionLabel; @@ -98,12 +104,14 @@ public class PrimaryCallState { this.isWifi = isWifi; this.isConference = isConference; this.isWorkCall = isWorkCall; + this.isHdAttempting = isHdAttempting; this.isHdAudioCall = isHdAudioCall; this.isForwardedNumber = isForwardedNumber; this.shouldShowContactPhoto = shouldShowContactPhoto; this.connectTimeMillis = connectTimeMillis; this.isVoiceMailNumber = isVoiceMailNumber; this.isRemotelyHeld = isRemotelyHeld; + this.isBusinessNumber = isBusinessNumber; } @Override diff --git a/java/com/android/incallui/incall/protocol/PrimaryInfo.java b/java/com/android/incallui/incall/protocol/PrimaryInfo.java index 1833ed22e..c1709501d 100644 --- a/java/com/android/incallui/incall/protocol/PrimaryInfo.java +++ b/java/com/android/incallui/incall/protocol/PrimaryInfo.java @@ -41,6 +41,7 @@ public class PrimaryInfo { // Used for consistent LetterTile coloring. @Nullable public final String contactInfoLookupKey; @Nullable public final MultimediaData multimediaData; + public final int numberPresentation; // TODO: Convert to autovalue. b/34502119 public static PrimaryInfo createEmptyPrimaryInfo() { @@ -59,7 +60,8 @@ public class PrimaryInfo { false, false, null, - null); + null, + -1); } public PrimaryInfo( @@ -77,7 +79,8 @@ public class PrimaryInfo { boolean answeringDisconnectsOngoingCall, boolean shouldShowLocation, @Nullable String contactInfoLookupKey, - @Nullable MultimediaData multimediaData) { + @Nullable MultimediaData multimediaData, + int numberPresentation) { this.number = number; this.name = name; this.nameIsNumber = nameIsNumber; @@ -93,6 +96,7 @@ public class PrimaryInfo { this.shouldShowLocation = shouldShowLocation; this.contactInfoLookupKey = contactInfoLookupKey; this.multimediaData = multimediaData; + this.numberPresentation = numberPresentation; } @Override diff --git a/java/com/android/incallui/maps/Maps.java b/java/com/android/incallui/maps/Maps.java new file mode 100644 index 000000000..648cf9f24 --- /dev/null +++ b/java/com/android/incallui/maps/Maps.java @@ -0,0 +1,33 @@ +/* + * 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.incallui.maps; + +import android.location.Location; +import android.support.annotation.NonNull; +import android.support.v4.app.Fragment; + +/** Used to create a fragment that can display a static map at the given location. */ +public interface Maps { + /** + * Used to check if maps is available. This will return false if Dialer was compiled without + * support for Google Play Services. + */ + boolean isAvailable(); + + @NonNull + Fragment createStaticMapFragment(@NonNull Location location); +} diff --git a/java/com/android/incallui/maps/MapsComponent.java b/java/com/android/incallui/maps/MapsComponent.java new file mode 100644 index 000000000..1ca17b781 --- /dev/null +++ b/java/com/android/incallui/maps/MapsComponent.java @@ -0,0 +1,49 @@ +/* + * 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.incallui.maps; + +import android.content.Context; +import com.android.dialer.inject.HasRootComponent; +import dagger.Subcomponent; +import com.android.incallui.maps.stub.StubMapsModule; + +/** Subcomponent that can be used to access the maps implementation. */ +public class MapsComponent { + + private static MapsComponent instance; + private Maps maps; + + public Maps getMaps() { + if (maps == null) { + maps = new StubMapsModule.StubMaps(); + } + return maps; + } + + public static MapsComponent get(Context context) { + if (instance == null) { + instance = new MapsComponent(); + } + return instance; + } + + + /** Used to refer to the root application component. */ + public interface HasComponent { + MapsComponent mapsComponent(); + } +} diff --git a/java/com/android/incallui/maps/StaticMapBinding.java b/java/com/android/incallui/maps/StaticMapBinding.java deleted file mode 100644 index 9d24ef27a..000000000 --- a/java/com/android/incallui/maps/StaticMapBinding.java +++ /dev/null @@ -1,51 +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.incallui.maps; - -import android.app.Application; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.support.annotation.VisibleForTesting; - -/** Utility for getting a {@link StaticMapFactory} */ -public class StaticMapBinding { - - @Nullable - public static StaticMapFactory get(@NonNull Application application) { - if (useTestingInstance) { - return testingInstance; - } - if (application instanceof StaticMapFactory) { - return ((StaticMapFactory) application); - } - return null; - } - - private static StaticMapFactory testingInstance; - private static boolean useTestingInstance; - - @VisibleForTesting - public static void setForTesting(@Nullable StaticMapFactory staticMapFactory) { - testingInstance = staticMapFactory; - useTestingInstance = true; - } - - @VisibleForTesting - public static void clearForTesting() { - useTestingInstance = false; - } -} diff --git a/java/com/android/incallui/maps/StaticMapFactory.java b/java/com/android/incallui/maps/StaticMapFactory.java deleted file mode 100644 index a35013886..000000000 --- a/java/com/android/incallui/maps/StaticMapFactory.java +++ /dev/null @@ -1,28 +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.incallui.maps; - -import android.location.Location; -import android.support.annotation.NonNull; -import android.support.v4.app.Fragment; - -/** A Factory that can create Fragments for showing a static map */ -public interface StaticMapFactory { - - @NonNull - Fragment getStaticMap(@NonNull Location location); -} diff --git a/java/com/android/incallui/maps/impl/AndroidManifest.xml b/java/com/android/incallui/maps/impl/AndroidManifest.xml new file mode 100644 index 000000000..4ad0b3b7e --- /dev/null +++ b/java/com/android/incallui/maps/impl/AndroidManifest.xml @@ -0,0 +1,26 @@ + + + + + + + + diff --git a/java/com/android/incallui/maps/impl/MapsImpl.java b/java/com/android/incallui/maps/impl/MapsImpl.java new file mode 100644 index 000000000..2cecee93e --- /dev/null +++ b/java/com/android/incallui/maps/impl/MapsImpl.java @@ -0,0 +1,40 @@ +/* + * 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.incallui.maps.impl; + +import android.location.Location; +import android.support.annotation.NonNull; +import android.support.v4.app.Fragment; +import com.android.incallui.maps.Maps; +import javax.inject.Inject; + +/** Uses Google Play Services APIs to create a static map fragment. */ +final class MapsImpl implements Maps { + @Inject + public MapsImpl() {} + + @Override + public boolean isAvailable() { + return true; + } + + @Override + @NonNull + public Fragment createStaticMapFragment(@NonNull Location location) { + return StaticMapFragment.newInstance(location); + } +} diff --git a/java/com/android/incallui/maps/impl/MapsModule.java b/java/com/android/incallui/maps/impl/MapsModule.java new file mode 100644 index 000000000..22f2f32a7 --- /dev/null +++ b/java/com/android/incallui/maps/impl/MapsModule.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.incallui.maps.impl; + +import com.android.incallui.maps.Maps; +import dagger.Binds; +import dagger.Module; +import javax.inject.Singleton; + +/** This module provides an instance of maps. */ +@Module +public abstract class MapsModule { + + @Binds + @Singleton + public abstract Maps bindMaps(MapsImpl maps); +} diff --git a/java/com/android/incallui/maps/impl/StaticMapFragment.java b/java/com/android/incallui/maps/impl/StaticMapFragment.java new file mode 100644 index 000000000..38a4c156b --- /dev/null +++ b/java/com/android/incallui/maps/impl/StaticMapFragment.java @@ -0,0 +1,76 @@ +/* + * 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.incallui.maps.impl; + +import android.location.Location; +import android.os.Bundle; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.support.v4.app.Fragment; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import com.android.dialer.common.Assert; +import com.android.dialer.common.LogUtil; +import com.google.android.gms.maps.CameraUpdateFactory; +import com.google.android.gms.maps.GoogleMap; +import com.google.android.gms.maps.OnMapReadyCallback; +import com.google.android.gms.maps.SupportMapFragment; +import com.google.android.gms.maps.model.LatLng; +import com.google.android.gms.maps.model.MarkerOptions; + +/** Shows a static map centered on a specified location */ +public class StaticMapFragment extends Fragment implements OnMapReadyCallback { + + private static final String ARG_LOCATION = "location"; + + public static StaticMapFragment newInstance(@NonNull Location location) { + Bundle args = new Bundle(); + args.putParcelable(ARG_LOCATION, Assert.isNotNull(location)); + StaticMapFragment fragment = new StaticMapFragment(); + fragment.setArguments(args); + return fragment; + } + + @Nullable + @Override + public View onCreateView( + LayoutInflater layoutInflater, @Nullable ViewGroup viewGroup, @Nullable Bundle bundle) { + return layoutInflater.inflate(R.layout.static_map_fragment, viewGroup, false); + } + + @Override + public void onViewCreated(View view, @Nullable Bundle bundle) { + super.onViewCreated(view, bundle); + SupportMapFragment mapFragment = + (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.static_map); + if (mapFragment != null) { + mapFragment.getMapAsync(this); + } else { + LogUtil.w("StaticMapFragment.onViewCreated", "No map fragment found!"); + } + } + + @Override + public void onMapReady(GoogleMap googleMap) { + Location location = getArguments().getParcelable(ARG_LOCATION); + LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude()); + googleMap.addMarker(new MarkerOptions().position(latLng).flat(true).draggable(false)); + googleMap.getUiSettings().setMapToolbarEnabled(false); + googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 15f)); + } +} diff --git a/java/com/android/incallui/maps/impl/res/layout/static_map_fragment.xml b/java/com/android/incallui/maps/impl/res/layout/static_map_fragment.xml new file mode 100644 index 000000000..54f41cb6e --- /dev/null +++ b/java/com/android/incallui/maps/impl/res/layout/static_map_fragment.xml @@ -0,0 +1,29 @@ + + + + + + diff --git a/java/com/android/incallui/maps/stub/StubMapsModule.java b/java/com/android/incallui/maps/stub/StubMapsModule.java new file mode 100644 index 000000000..72678143c --- /dev/null +++ b/java/com/android/incallui/maps/stub/StubMapsModule.java @@ -0,0 +1,52 @@ +/* + * 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.incallui.maps.stub; + +import android.location.Location; +import android.support.annotation.NonNull; +import android.support.v4.app.Fragment; +import com.android.dialer.common.Assert; +import com.android.incallui.maps.Maps; +import dagger.Binds; +import dagger.Module; +import javax.inject.Inject; +import javax.inject.Singleton; + +/** Stub for the maps module for build variants that don't support Google Play Services. */ +@Module +public abstract class StubMapsModule { + + @Binds + @Singleton + public abstract Maps bindMaps(StubMaps maps); + + static public final class StubMaps implements Maps { + @Inject + public StubMaps() {} + + @Override + public boolean isAvailable() { + return false; + } + + @NonNull + @Override + public Fragment createStaticMapFragment(@NonNull Location location) { + throw Assert.createUnsupportedOperationFailException(); + } + } +} diff --git a/java/com/android/incallui/maps/testing/TestMapsModule.java b/java/com/android/incallui/maps/testing/TestMapsModule.java new file mode 100644 index 000000000..bb096812b --- /dev/null +++ b/java/com/android/incallui/maps/testing/TestMapsModule.java @@ -0,0 +1,40 @@ +/* + * 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.incallui.maps.testing; + +import android.support.annotation.Nullable; +import com.android.incallui.maps.Maps; +import dagger.Module; +import dagger.Provides; + +/** This module provides a instance of maps for testing. */ +@Module +public final class TestMapsModule { + + @Nullable private static Maps maps; + + public static void setMaps(@Nullable Maps maps) { + TestMapsModule.maps = maps; + } + + @Provides + static Maps getMaps() { + return maps; + } + + private TestMapsModule() {} +} diff --git a/java/com/android/incallui/res/values/strings.xml b/java/com/android/incallui/res/values/strings.xml index 252d131de..0b95a9cc6 100644 --- a/java/com/android/incallui/res/values/strings.xml +++ b/java/com/android/incallui/res/values/strings.xml @@ -223,17 +223,6 @@ ABSENTNUMBER - - Service - - - Setup - - - <Not set> - Other call settings @@ -242,26 +231,6 @@ select contact - - Vibrate - - Vibrate - - - Sound - - - never - - - - always - silent - never - - diff --git a/java/com/android/incallui/sessiondata/MultimediaFragment.java b/java/com/android/incallui/sessiondata/MultimediaFragment.java index d6f671d58..14aa0a3aa 100644 --- a/java/com/android/incallui/sessiondata/MultimediaFragment.java +++ b/java/com/android/incallui/sessiondata/MultimediaFragment.java @@ -31,12 +31,10 @@ import android.view.ViewGroup; import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.TextView; -import com.android.dialer.common.Assert; import com.android.dialer.common.FragmentUtils; import com.android.dialer.common.LogUtil; import com.android.dialer.multimedia.MultimediaData; -import com.android.incallui.maps.StaticMapBinding; -import com.android.incallui.maps.StaticMapFactory; +import com.android.incallui.maps.MapsComponent; import com.bumptech.glide.Glide; import com.bumptech.glide.load.DataSource; import com.bumptech.glide.load.engine.GlideException; @@ -58,17 +56,13 @@ public class MultimediaFragment extends Fragment implements AvatarPresenter { private static final String ARG_INTERACTIVE = "interactive"; private static final String ARG_SHOW_AVATAR = "show_avatar"; private ImageView avatarImageView; - // TODO: add click listeners - @SuppressWarnings("unused") - private boolean isInteractive; private boolean showAvatar; - private StaticMapFactory mapFactory; public static MultimediaFragment newInstance( @NonNull MultimediaData multimediaData, boolean isInteractive, boolean showAvatar) { return newInstance( - multimediaData.getSubject(), + multimediaData.getText(), multimediaData.getImageUri(), multimediaData.getLocation(), isInteractive, @@ -96,7 +90,6 @@ public class MultimediaFragment extends Fragment implements AvatarPresenter { @Override public void onCreate(@Nullable Bundle bundle) { super.onCreate(bundle); - isInteractive = getArguments().getBoolean(ARG_INTERACTIVE); showAvatar = getArguments().getBoolean(ARG_SHOW_AVATAR); } @@ -107,10 +100,7 @@ public class MultimediaFragment extends Fragment implements AvatarPresenter { boolean hasImage = getImageUri() != null; boolean hasSubject = !TextUtils.isEmpty(getSubject()); boolean hasMap = getLocation() != null; - if (hasMap) { - mapFactory = StaticMapBinding.get(getActivity().getApplication()); - } - if (mapFactory != null) { + if (hasMap && MapsComponent.get(getContext()).getMaps().isAvailable()) { if (hasImage) { if (hasSubject) { return layoutInflater.inflate( @@ -178,7 +168,7 @@ public class MultimediaFragment extends Fragment implements AvatarPresenter { if (fragmentHolder != null) { fragmentHolder.setClipToOutline(true); Fragment mapFragment = - Assert.isNotNull(mapFactory).getStaticMap(Assert.isNotNull(getLocation())); + MapsComponent.get(getContext()).getMaps().createStaticMapFragment(getLocation()); getChildFragmentManager() .beginTransaction() .replace(R.id.answer_message_frag, mapFragment) diff --git a/java/com/android/incallui/sessiondata/res/layout/fragment_composer_image.xml b/java/com/android/incallui/sessiondata/res/layout/fragment_composer_image.xml index 7000f83b5..0882781e7 100644 --- a/java/com/android/incallui/sessiondata/res/layout/fragment_composer_image.xml +++ b/java/com/android/incallui/sessiondata/res/layout/fragment_composer_image.xml @@ -46,5 +46,6 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/loading_spinner" - android:layout_centerInParent="true"/> + android:layout_centerInParent="true" + android:elevation="2dp"/> diff --git a/java/com/android/incallui/sessiondata/res/layout/fragment_composer_image_frag.xml b/java/com/android/incallui/sessiondata/res/layout/fragment_composer_image_frag.xml index 9959f4dcc..c816418fc 100644 --- a/java/com/android/incallui/sessiondata/res/layout/fragment_composer_image_frag.xml +++ b/java/com/android/incallui/sessiondata/res/layout/fragment_composer_image_frag.xml @@ -42,6 +42,14 @@ android:outlineProvider="background" android:scaleType="centerCrop"/> + + + + diff --git a/java/com/android/incallui/sessiondata/res/layout/fragment_composer_text_image_frag.xml b/java/com/android/incallui/sessiondata/res/layout/fragment_composer_text_image_frag.xml index 387c5cf68..ffbe41bbd 100644 --- a/java/com/android/incallui/sessiondata/res/layout/fragment_composer_text_image_frag.xml +++ b/java/com/android/incallui/sessiondata/res/layout/fragment_composer_text_image_frag.xml @@ -61,6 +61,14 @@ android:outlineProvider="background" android:scaleType="centerCrop"/> + + + android:background="@color/videocall_overlay_background_color" + tools:visibility="gone"/> + android:background="@color/videocall_overlay_background_color" + tools:visibility="gone"/> diff --git a/java/com/android/incallui/video/impl/res/values/colors.xml b/java/com/android/incallui/video/impl/res/values/colors.xml new file mode 100644 index 000000000..874bf9404 --- /dev/null +++ b/java/com/android/incallui/video/impl/res/values/colors.xml @@ -0,0 +1,20 @@ + + + + + #89ffffff + diff --git a/java/com/android/incallui/video/protocol/VideoCallScreen.java b/java/com/android/incallui/video/protocol/VideoCallScreen.java index 0eaf692e2..bad050cd1 100644 --- a/java/com/android/incallui/video/protocol/VideoCallScreen.java +++ b/java/com/android/incallui/video/protocol/VideoCallScreen.java @@ -21,6 +21,10 @@ import android.support.v4.app.Fragment; /** Interface for call video call module. */ public interface VideoCallScreen { + void onVideoScreenStart(); + + void onVideoScreenStop(); + void showVideoViews(boolean shouldShowPreview, boolean shouldShowRemote, boolean isRemotelyHeld); void onLocalVideoDimensionsChanged(); @@ -33,4 +37,6 @@ public interface VideoCallScreen { boolean shouldShowFullscreen, boolean shouldShowGreenScreen); Fragment getVideoCallScreenFragment(); + + String getCallId(); } diff --git a/java/com/android/incallui/videotech/VideoTech.java b/java/com/android/incallui/videotech/VideoTech.java new file mode 100644 index 000000000..fb2641793 --- /dev/null +++ b/java/com/android/incallui/videotech/VideoTech.java @@ -0,0 +1,96 @@ +/* + * 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.incallui.videotech; + +import android.support.annotation.IntDef; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +/** Video calling interface. */ +public interface VideoTech { + + boolean isAvailable(); + + boolean isTransmittingOrReceiving(); + + void onCallStateChanged(int newState); + + @SessionModificationState + int getSessionModificationState(); + + void upgradeToVideo(); + + void acceptVideoRequest(); + + void acceptVideoRequestAsAudio(); + + void declineVideoRequest(); + + boolean isTransmitting(); + + void stopTransmission(); + + void resumeTransmission(); + + void pause(); + + void unpause(); + + void setCamera(String cameraId); + + void setDeviceOrientation(int rotation); + + /** Listener for video call events. */ + interface VideoTechListener { + + void onVideoTechStateChanged(); + + void onSessionModificationStateChanged(); + + void onCameraDimensionsChanged(int width, int height); + + void onPeerDimensionsChanged(int width, int height); + + void onVideoUpgradeRequestReceived(); + } + + /** + * Defines different states of session modify requests, which are used to upgrade to video, or + * downgrade to audio. + */ + @Retention(RetentionPolicy.SOURCE) + @IntDef({ + SESSION_MODIFICATION_STATE_NO_REQUEST, + SESSION_MODIFICATION_STATE_WAITING_FOR_UPGRADE_TO_VIDEO_RESPONSE, + SESSION_MODIFICATION_STATE_REQUEST_FAILED, + SESSION_MODIFICATION_STATE_RECEIVED_UPGRADE_TO_VIDEO_REQUEST, + SESSION_MODIFICATION_STATE_UPGRADE_TO_VIDEO_REQUEST_TIMED_OUT, + SESSION_MODIFICATION_STATE_UPGRADE_TO_VIDEO_REQUEST_FAILED, + SESSION_MODIFICATION_STATE_REQUEST_REJECTED, + SESSION_MODIFICATION_STATE_WAITING_FOR_RESPONSE + }) + @interface SessionModificationState {} + + int SESSION_MODIFICATION_STATE_NO_REQUEST = 0; + int SESSION_MODIFICATION_STATE_WAITING_FOR_UPGRADE_TO_VIDEO_RESPONSE = 1; + int SESSION_MODIFICATION_STATE_REQUEST_FAILED = 2; + int SESSION_MODIFICATION_STATE_RECEIVED_UPGRADE_TO_VIDEO_REQUEST = 3; + int SESSION_MODIFICATION_STATE_UPGRADE_TO_VIDEO_REQUEST_TIMED_OUT = 4; + int SESSION_MODIFICATION_STATE_UPGRADE_TO_VIDEO_REQUEST_FAILED = 5; + int SESSION_MODIFICATION_STATE_REQUEST_REJECTED = 6; + int SESSION_MODIFICATION_STATE_WAITING_FOR_RESPONSE = 7; +} diff --git a/java/com/android/incallui/videotech/empty/EmptyVideoTech.java b/java/com/android/incallui/videotech/empty/EmptyVideoTech.java new file mode 100644 index 000000000..bc8db4c07 --- /dev/null +++ b/java/com/android/incallui/videotech/empty/EmptyVideoTech.java @@ -0,0 +1,76 @@ +/* + * 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.incallui.videotech.empty; + +import com.android.incallui.videotech.VideoTech; + +/** Default video tech that is always available but doesn't do anything. */ +public class EmptyVideoTech implements VideoTech { + + @Override + public boolean isAvailable() { + return false; + } + + @Override + public boolean isTransmittingOrReceiving() { + return false; + } + + @Override + public void onCallStateChanged(int newState) {} + + @Override + public int getSessionModificationState() { + return VideoTech.SESSION_MODIFICATION_STATE_NO_REQUEST; + } + + @Override + public void upgradeToVideo() {} + + @Override + public void acceptVideoRequest() {} + + @Override + public void acceptVideoRequestAsAudio() {} + + @Override + public void declineVideoRequest() {} + + @Override + public boolean isTransmitting() { + return false; + } + + @Override + public void stopTransmission() {} + + @Override + public void resumeTransmission() {} + + @Override + public void pause() {} + + @Override + public void unpause() {} + + @Override + public void setCamera(String cameraId) {} + + @Override + public void setDeviceOrientation(int rotation) {} +} diff --git a/java/com/android/incallui/videotech/ims/ImsVideoCallCallback.java b/java/com/android/incallui/videotech/ims/ImsVideoCallCallback.java new file mode 100644 index 000000000..0a15f7e65 --- /dev/null +++ b/java/com/android/incallui/videotech/ims/ImsVideoCallCallback.java @@ -0,0 +1,201 @@ +/* + * 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.incallui.videotech.ims; + +import android.os.Handler; +import android.telecom.Call; +import android.telecom.Connection; +import android.telecom.Connection.VideoProvider; +import android.telecom.InCallService.VideoCall; +import android.telecom.VideoProfile; +import android.telecom.VideoProfile.CameraCapabilities; +import com.android.dialer.common.LogUtil; +import com.android.incallui.videotech.VideoTech; +import com.android.incallui.videotech.VideoTech.SessionModificationState; +import com.android.incallui.videotech.VideoTech.VideoTechListener; + +/** Receives IMS video call state updates. */ +public class ImsVideoCallCallback extends VideoCall.Callback { + private static final int CLEAR_FAILED_REQUEST_TIMEOUT_MILLIS = 4000; + private final Handler handler = new Handler(); + private final Call call; + private final ImsVideoTech videoTech; + private final VideoTechListener listener; + private int requestedVideoState = VideoProfile.STATE_AUDIO_ONLY; + + ImsVideoCallCallback(final Call call, ImsVideoTech videoTech, VideoTechListener listener) { + this.call = call; + this.videoTech = videoTech; + this.listener = listener; + } + + @Override + public void onSessionModifyRequestReceived(VideoProfile videoProfile) { + LogUtil.i( + "ImsVideoCallCallback.onSessionModifyRequestReceived", "videoProfile: " + videoProfile); + + int previousVideoState = ImsVideoTech.getUnpausedVideoState(call.getDetails().getVideoState()); + int newVideoState = ImsVideoTech.getUnpausedVideoState(videoProfile.getVideoState()); + + boolean wasVideoCall = VideoProfile.isVideo(previousVideoState); + boolean isVideoCall = VideoProfile.isVideo(newVideoState); + + if (wasVideoCall && !isVideoCall) { + LogUtil.i( + "ImsVideoTech.onSessionModifyRequestReceived", "call downgraded to %d", newVideoState); + } else if (previousVideoState != newVideoState) { + requestedVideoState = newVideoState; + videoTech.setSessionModificationState( + VideoTech.SESSION_MODIFICATION_STATE_RECEIVED_UPGRADE_TO_VIDEO_REQUEST); + listener.onVideoUpgradeRequestReceived(); + } + } + + /** + * @param status Status of the session modify request. Valid values are {@link + * Connection.VideoProvider#SESSION_MODIFY_REQUEST_SUCCESS}, {@link + * Connection.VideoProvider#SESSION_MODIFY_REQUEST_FAIL}, {@link + * Connection.VideoProvider#SESSION_MODIFY_REQUEST_INVALID} + * @param responseProfile The actual profile changes made by the peer device. + */ + @Override + public void onSessionModifyResponseReceived( + int status, VideoProfile requestedProfile, VideoProfile responseProfile) { + LogUtil.i( + "ImsVideoCallCallback.onSessionModifyResponseReceived", + "status: %d, requestedProfile: %s, responseProfile: %s, session modification state: %d", + status, + requestedProfile, + responseProfile, + videoTech.getSessionModificationState()); + + if (videoTech.getSessionModificationState() + == VideoTech.SESSION_MODIFICATION_STATE_WAITING_FOR_UPGRADE_TO_VIDEO_RESPONSE) { + handler.removeCallbacksAndMessages(null); // Clear everything + + final int newSessionModificationState = getSessionModificationStateFromTelecomStatus(status); + if (status != VideoProvider.SESSION_MODIFY_REQUEST_SUCCESS) { + // This will update the video UI to display the error message. + videoTech.setSessionModificationState(newSessionModificationState); + } + + // Wait for 4 seconds and then clean the session modification state. This allows the video UI + // to stay up so that the user can read the error message. + // + // If the other person accepted the upgrade request then this will keep the video UI up until + // the call's video state change. Without this we would switch to the voice call and then + // switch back to video UI. + handler.postDelayed( + () -> { + if (videoTech.getSessionModificationState() == newSessionModificationState) { + LogUtil.i("ImsVideoCallCallback.onSessionModifyResponseReceived", "clearing state"); + videoTech.setSessionModificationState( + VideoTech.SESSION_MODIFICATION_STATE_NO_REQUEST); + } else { + LogUtil.i( + "ImsVideoCallCallback.onSessionModifyResponseReceived", + "session modification state has changed, not clearing state"); + } + }, + CLEAR_FAILED_REQUEST_TIMEOUT_MILLIS); + } else if (videoTech.getSessionModificationState() + == VideoTech.SESSION_MODIFICATION_STATE_RECEIVED_UPGRADE_TO_VIDEO_REQUEST) { + videoTech.setSessionModificationState(VideoTech.SESSION_MODIFICATION_STATE_NO_REQUEST); + } else if (videoTech.getSessionModificationState() + == VideoTech.SESSION_MODIFICATION_STATE_WAITING_FOR_RESPONSE) { + videoTech.setSessionModificationState(getSessionModificationStateFromTelecomStatus(status)); + } else { + LogUtil.i( + "ImsVideoCallCallback.onSessionModifyResponseReceived", + "call is not waiting for response, doing nothing"); + } + } + + @SessionModificationState + private int getSessionModificationStateFromTelecomStatus(int telecomStatus) { + switch (telecomStatus) { + case VideoProvider.SESSION_MODIFY_REQUEST_SUCCESS: + return VideoTech.SESSION_MODIFICATION_STATE_NO_REQUEST; + case VideoProvider.SESSION_MODIFY_REQUEST_FAIL: + case VideoProvider.SESSION_MODIFY_REQUEST_INVALID: + // Check if it's already video call, which means the request is not video upgrade request. + if (VideoProfile.isVideo(call.getDetails().getVideoState())) { + return VideoTech.SESSION_MODIFICATION_STATE_REQUEST_FAILED; + } else { + return VideoTech.SESSION_MODIFICATION_STATE_UPGRADE_TO_VIDEO_REQUEST_FAILED; + } + case VideoProvider.SESSION_MODIFY_REQUEST_TIMED_OUT: + return VideoTech.SESSION_MODIFICATION_STATE_UPGRADE_TO_VIDEO_REQUEST_TIMED_OUT; + case VideoProvider.SESSION_MODIFY_REQUEST_REJECTED_BY_REMOTE: + return VideoTech.SESSION_MODIFICATION_STATE_REQUEST_REJECTED; + default: + LogUtil.e( + "ImsVideoCallCallback.getSessionModificationStateFromTelecomStatus", + "unknown status: %d", + telecomStatus); + return VideoTech.SESSION_MODIFICATION_STATE_REQUEST_FAILED; + } + } + + @Override + public void onCallSessionEvent(int event) { + switch (event) { + case Connection.VideoProvider.SESSION_EVENT_RX_PAUSE: + LogUtil.i("ImsVideoCallCallback.onCallSessionEvent", "rx_pause"); + break; + case Connection.VideoProvider.SESSION_EVENT_RX_RESUME: + LogUtil.i("ImsVideoCallCallback.onCallSessionEvent", "rx_resume"); + break; + case Connection.VideoProvider.SESSION_EVENT_CAMERA_FAILURE: + LogUtil.i("ImsVideoCallCallback.onCallSessionEvent", "camera_failure"); + break; + case Connection.VideoProvider.SESSION_EVENT_CAMERA_READY: + LogUtil.i("ImsVideoCallCallback.onCallSessionEvent", "camera_ready"); + break; + default: + LogUtil.i("ImsVideoCallCallback.onCallSessionEvent", "unknown event = : " + event); + break; + } + } + + @Override + public void onPeerDimensionsChanged(int width, int height) { + listener.onPeerDimensionsChanged(width, height); + } + + @Override + public void onVideoQualityChanged(int videoQuality) { + LogUtil.i("ImsVideoCallCallback.onVideoQualityChanged", "videoQuality: %d", videoQuality); + } + + @Override + public void onCallDataUsageChanged(long dataUsage) { + LogUtil.i("ImsVideoCallCallback.onCallDataUsageChanged", "dataUsage: %d", dataUsage); + } + + @Override + public void onCameraCapabilitiesChanged(CameraCapabilities cameraCapabilities) { + if (cameraCapabilities != null) { + listener.onCameraDimensionsChanged( + cameraCapabilities.getWidth(), cameraCapabilities.getHeight()); + } + } + + int getRequestedVideoState() { + return requestedVideoState; + } +} diff --git a/java/com/android/incallui/videotech/ims/ImsVideoTech.java b/java/com/android/incallui/videotech/ims/ImsVideoTech.java new file mode 100644 index 000000000..890e5c80c --- /dev/null +++ b/java/com/android/incallui/videotech/ims/ImsVideoTech.java @@ -0,0 +1,212 @@ +/* + * 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.incallui.videotech.ims; + +import android.os.Build; +import android.telecom.Call; +import android.telecom.Call.Details; +import android.telecom.VideoProfile; +import com.android.dialer.common.Assert; +import com.android.dialer.common.LogUtil; +import com.android.incallui.videotech.VideoTech; + +/** ViLTE implementation */ +public class ImsVideoTech implements VideoTech { + private final Call call; + private final VideoTechListener listener; + private ImsVideoCallCallback callback; + private @SessionModificationState int sessionModificationState = + VideoTech.SESSION_MODIFICATION_STATE_NO_REQUEST; + private int previousVideoState = VideoProfile.STATE_AUDIO_ONLY; + + public ImsVideoTech(VideoTechListener listener, Call call) { + this.listener = listener; + this.call = call; + } + + @Override + public boolean isAvailable() { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { + return false; + } + + boolean hasCapabilities = + call.getDetails().can(Call.Details.CAPABILITY_SUPPORTS_VT_LOCAL_TX) + && call.getDetails().can(Call.Details.CAPABILITY_SUPPORTS_VT_REMOTE_RX); + + return call.getVideoCall() != null + && (hasCapabilities || VideoProfile.isVideo(call.getDetails().getVideoState())); + } + + @Override + public boolean isTransmittingOrReceiving() { + return VideoProfile.isVideo(call.getDetails().getVideoState()); + } + + @Override + public void onCallStateChanged(int newState) { + if (!isAvailable()) { + return; + } + + if (callback == null) { + callback = new ImsVideoCallCallback(call, this, listener); + call.getVideoCall().registerCallback(callback); + } + + if (getSessionModificationState() + == VideoTech.SESSION_MODIFICATION_STATE_WAITING_FOR_UPGRADE_TO_VIDEO_RESPONSE + && isTransmittingOrReceiving()) { + // We don't clear the session modification state right away when we find out the video upgrade + // request was accepted to avoid having the UI switch from video to voice to video. + // Once the underlying telecom call updates to video mode it's safe to clear the state. + LogUtil.i( + "ImsVideoTech.onCallStateChanged", + "upgraded to video, clearing session modification state"); + setSessionModificationState(VideoTech.SESSION_MODIFICATION_STATE_NO_REQUEST); + } + + // Determines if a received upgrade to video request should be cancelled. This can happen if + // another InCall UI responds to the upgrade to video request. + int newVideoState = call.getDetails().getVideoState(); + if (newVideoState != previousVideoState + && sessionModificationState + == VideoTech.SESSION_MODIFICATION_STATE_RECEIVED_UPGRADE_TO_VIDEO_REQUEST) { + LogUtil.i("ImsVideoTech.onCallStateChanged", "cancelling upgrade notification"); + setSessionModificationState(VideoTech.SESSION_MODIFICATION_STATE_NO_REQUEST); + } + previousVideoState = newVideoState; + } + + @Override + public int getSessionModificationState() { + return sessionModificationState; + } + + void setSessionModificationState(@SessionModificationState int state) { + if (state != sessionModificationState) { + LogUtil.i( + "ImsVideoTech.setSessionModificationState", "%d -> %d", sessionModificationState, state); + sessionModificationState = state; + listener.onSessionModificationStateChanged(); + } + } + + @Override + public void upgradeToVideo() { + LogUtil.enterBlock("ImsVideoTech.upgradeToVideo"); + + int unpausedVideoState = getUnpausedVideoState(call.getDetails().getVideoState()); + call.getVideoCall() + .sendSessionModifyRequest( + new VideoProfile(unpausedVideoState | VideoProfile.STATE_BIDIRECTIONAL)); + setSessionModificationState( + VideoTech.SESSION_MODIFICATION_STATE_WAITING_FOR_UPGRADE_TO_VIDEO_RESPONSE); + } + + @Override + public void acceptVideoRequest() { + int requestedVideoState = callback.getRequestedVideoState(); + Assert.checkArgument(requestedVideoState != VideoProfile.STATE_AUDIO_ONLY); + LogUtil.i("ImsVideoTech.acceptUpgradeRequest", "videoState: " + requestedVideoState); + call.getVideoCall().sendSessionModifyResponse(new VideoProfile(requestedVideoState)); + setSessionModificationState(VideoTech.SESSION_MODIFICATION_STATE_NO_REQUEST); + } + + @Override + public void acceptVideoRequestAsAudio() { + LogUtil.enterBlock("ImsVideoTech.acceptVideoRequestAsAudio"); + call.getVideoCall().sendSessionModifyResponse(new VideoProfile(VideoProfile.STATE_AUDIO_ONLY)); + setSessionModificationState(VideoTech.SESSION_MODIFICATION_STATE_NO_REQUEST); + } + + @Override + public void declineVideoRequest() { + LogUtil.enterBlock("ImsVideoTech.declineUpgradeRequest"); + call.getVideoCall() + .sendSessionModifyResponse(new VideoProfile(call.getDetails().getVideoState())); + setSessionModificationState(VideoTech.SESSION_MODIFICATION_STATE_NO_REQUEST); + } + + @Override + public boolean isTransmitting() { + return VideoProfile.isTransmissionEnabled(call.getDetails().getVideoState()); + } + + @Override + public void stopTransmission() { + LogUtil.enterBlock("ImsVideoTech.stopTransmission"); + + int unpausedVideoState = getUnpausedVideoState(call.getDetails().getVideoState()); + call.getVideoCall() + .sendSessionModifyRequest( + new VideoProfile(unpausedVideoState & ~VideoProfile.STATE_TX_ENABLED)); + } + + @Override + public void resumeTransmission() { + LogUtil.enterBlock("ImsVideoTech.resumeTransmission"); + + int unpausedVideoState = getUnpausedVideoState(call.getDetails().getVideoState()); + call.getVideoCall() + .sendSessionModifyRequest( + new VideoProfile(unpausedVideoState | VideoProfile.STATE_TX_ENABLED)); + setSessionModificationState(VideoTech.SESSION_MODIFICATION_STATE_WAITING_FOR_RESPONSE); + } + + @Override + public void pause() { + if (canPause()) { + LogUtil.i("ImsVideoTech.pause", "sending pause request"); + int pausedVideoState = call.getDetails().getVideoState() | VideoProfile.STATE_PAUSED; + call.getVideoCall().sendSessionModifyRequest(new VideoProfile(pausedVideoState)); + } else { + LogUtil.i("ImsVideoTech.pause", "not sending request: canPause: %b", canPause()); + } + } + + @Override + public void unpause() { + if (canPause()) { + LogUtil.i("ImsVideoTech.unpause", "sending unpause request"); + int unpausedVideoState = getUnpausedVideoState(call.getDetails().getVideoState()); + call.getVideoCall().sendSessionModifyRequest(new VideoProfile(unpausedVideoState)); + } else { + LogUtil.i("ImsVideoTech.unpause", "not sending request: canPause: %b", canPause()); + } + } + + @Override + public void setCamera(String cameraId) { + call.getVideoCall().setCamera(cameraId); + call.getVideoCall().requestCameraCapabilities(); + } + + @Override + public void setDeviceOrientation(int rotation) { + call.getVideoCall().setDeviceOrientation(rotation); + } + + private boolean canPause() { + return call.getDetails().can(Details.CAPABILITY_CAN_PAUSE_VIDEO) + && call.getState() == Call.STATE_ACTIVE; + } + + static int getUnpausedVideoState(int videoState) { + return videoState & (~VideoProfile.STATE_PAUSED); + } +} diff --git a/java/com/android/incallui/videotech/rcs/RcsVideoShare.java b/java/com/android/incallui/videotech/rcs/RcsVideoShare.java new file mode 100644 index 000000000..2cb43036f --- /dev/null +++ b/java/com/android/incallui/videotech/rcs/RcsVideoShare.java @@ -0,0 +1,195 @@ +/* + * 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.incallui.videotech.rcs; + +import android.support.annotation.NonNull; +import android.telecom.Call; +import com.android.dialer.common.Assert; +import com.android.dialer.common.LogUtil; +import com.android.dialer.enrichedcall.EnrichedCallCapabilities; +import com.android.dialer.enrichedcall.EnrichedCallManager; +import com.android.dialer.enrichedcall.EnrichedCallManager.CapabilitiesListener; +import com.android.dialer.enrichedcall.Session; +import com.android.dialer.enrichedcall.videoshare.VideoShareListener; +import com.android.incallui.videotech.VideoTech; + +/** Allows the in-call UI to make video calls over RCS. */ +public class RcsVideoShare implements VideoTech, CapabilitiesListener, VideoShareListener { + private final EnrichedCallManager enrichedCallManager; + private final VideoTechListener listener; + private final String callingNumber; + private int previousCallState = Call.STATE_NEW; + private long inviteSessionId = Session.NO_SESSION_ID; + private long transmittingSessionId = Session.NO_SESSION_ID; + private long receivingSessionId = Session.NO_SESSION_ID; + + private @SessionModificationState int sessionModificationState = + VideoTech.SESSION_MODIFICATION_STATE_NO_REQUEST; + + public RcsVideoShare( + @NonNull EnrichedCallManager enrichedCallManager, + @NonNull VideoTechListener listener, + @NonNull String callingNumber) { + this.enrichedCallManager = Assert.isNotNull(enrichedCallManager); + this.listener = Assert.isNotNull(listener); + this.callingNumber = Assert.isNotNull(callingNumber); + + enrichedCallManager.registerCapabilitiesListener(this); + enrichedCallManager.registerVideoShareListener(this); + } + + @Override + public boolean isAvailable() { + EnrichedCallCapabilities capabilities = enrichedCallManager.getCapabilities(callingNumber); + return capabilities != null && capabilities.supportsVideoShare(); + } + + @Override + public boolean isTransmittingOrReceiving() { + return transmittingSessionId != Session.NO_SESSION_ID + || receivingSessionId != Session.NO_SESSION_ID; + } + + @Override + public void onCallStateChanged(int newState) { + if (newState == Call.STATE_DISCONNECTING) { + enrichedCallManager.unregisterVideoShareListener(this); + enrichedCallManager.unregisterCapabilitiesListener(this); + } + + if (newState != previousCallState && newState == Call.STATE_ACTIVE) { + // Per spec, request capabilities when the call becomes active + enrichedCallManager.requestCapabilities(callingNumber); + } + + previousCallState = newState; + } + + @Override + public int getSessionModificationState() { + return sessionModificationState; + } + + private void setSessionModificationState(@SessionModificationState int state) { + if (state != sessionModificationState) { + LogUtil.i( + "RcsVideoShare.setSessionModificationState", "%d -> %d", sessionModificationState, state); + sessionModificationState = state; + listener.onSessionModificationStateChanged(); + } + } + + @Override + public void upgradeToVideo() { + LogUtil.enterBlock("RcsVideoShare.upgradeToVideo"); + transmittingSessionId = enrichedCallManager.startVideoShareSession(callingNumber); + if (transmittingSessionId != Session.NO_SESSION_ID) { + setSessionModificationState( + VideoTech.SESSION_MODIFICATION_STATE_WAITING_FOR_UPGRADE_TO_VIDEO_RESPONSE); + } + } + + @Override + public void acceptVideoRequest() { + LogUtil.enterBlock("RcsVideoShare.acceptVideoRequest"); + if (enrichedCallManager.acceptVideoShareSession(inviteSessionId)) { + receivingSessionId = inviteSessionId; + } + inviteSessionId = Session.NO_SESSION_ID; + setSessionModificationState(VideoTech.SESSION_MODIFICATION_STATE_NO_REQUEST); + } + + @Override + public void acceptVideoRequestAsAudio() { + throw Assert.createUnsupportedOperationFailException(); + } + + @Override + public void declineVideoRequest() { + LogUtil.enterBlock("RcsVideoTech.declineUpgradeRequest"); + enrichedCallManager.endVideoShareSession( + enrichedCallManager.getVideoShareInviteSessionId(callingNumber)); + inviteSessionId = Session.NO_SESSION_ID; + setSessionModificationState(VideoTech.SESSION_MODIFICATION_STATE_NO_REQUEST); + } + + @Override + public boolean isTransmitting() { + return transmittingSessionId != Session.NO_SESSION_ID; + } + + @Override + public void stopTransmission() { + LogUtil.enterBlock("RcsVideoTech.stopTransmission"); + } + + @Override + public void resumeTransmission() { + LogUtil.enterBlock("RcsVideoTech.resumeTransmission"); + } + + @Override + public void pause() {} + + @Override + public void unpause() {} + + @Override + public void setCamera(String cameraId) {} + + @Override + public void setDeviceOrientation(int rotation) {} + + @Override + public void onCapabilitiesUpdated() { + listener.onVideoTechStateChanged(); + } + + @Override + public void onVideoShareChanged() { + long existingInviteSessionId = inviteSessionId; + + inviteSessionId = enrichedCallManager.getVideoShareInviteSessionId(callingNumber); + if (inviteSessionId != Session.NO_SESSION_ID) { + if (existingInviteSessionId == Session.NO_SESSION_ID) { + // This is a new invite + setSessionModificationState( + VideoTech.SESSION_MODIFICATION_STATE_RECEIVED_UPGRADE_TO_VIDEO_REQUEST); + listener.onVideoUpgradeRequestReceived(); + } + } else { + setSessionModificationState(VideoTech.SESSION_MODIFICATION_STATE_NO_REQUEST); + } + + if (sessionIsClosed(transmittingSessionId)) { + LogUtil.i("RcsVideoShare.onSessionClosed", "transmitting session closed"); + transmittingSessionId = Session.NO_SESSION_ID; + } + + if (sessionIsClosed(receivingSessionId)) { + LogUtil.i("RcsVideoShare.onSessionClosed", "receiving session closed"); + receivingSessionId = Session.NO_SESSION_ID; + } + + listener.onVideoTechStateChanged(); + } + + private boolean sessionIsClosed(long sessionId) { + return sessionId != Session.NO_SESSION_ID + && enrichedCallManager.getVideoShareSession(sessionId) == null; + } +} diff --git a/java/com/android/voicemail/VoicemailClient.java b/java/com/android/voicemail/VoicemailClient.java new file mode 100644 index 000000000..b237f65f6 --- /dev/null +++ b/java/com/android/voicemail/VoicemailClient.java @@ -0,0 +1,60 @@ +/* + * 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.voicemail; + +import android.content.Context; +import android.provider.VoicemailContract.Voicemails; +import android.support.annotation.Nullable; +import android.telecom.PhoneAccountHandle; +import android.telephony.TelephonyManager; +import java.util.List; + +/** Public interface for the voicemail module */ +public interface VoicemailClient { + + /** + * Broadcast to tell the client to upload local database changes to the server. Since the dialer + * UI and the client are in the same package, the {@link + * android.content.Intent#ACTION_PROVIDER_CHANGED} will always be a self-change even if the UI is + * external to the client. + */ + String ACTION_UPLOAD = "com.android.voicemailomtp.VoicemailClient.ACTION_UPLOAD"; + + /** + * Appends the selection to ignore voicemails from non-active OMTP voicemail package. In OC there + * can be multiple packages handling OMTP voicemails which represents the same source of truth. + * These packages should mark their voicemails as {@link Voicemails#IS_OMTP_VOICEMAIL} and only + * the voicemails from {@link TelephonyManager#getVisualVoicemailPackageName()} should be shown. + * For example, the user synced voicemails with DialerA, and then switched to DialerB, voicemails + * from DialerA should be ignored as they are no longer current. Voicemails from {@link + * #OMTP_VOICEMAIL_BLACKLIST} will also be ignored as they are voicemail source only valid pre-OC. + */ + void appendOmtpVoicemailSelectionClause( + Context context, StringBuilder where, List selectionArgs); + /** + * @return the class name of the {@link android.preference.PreferenceFragment} for voicemail + * settings, or {@code null} if dialer cannot control voicemail settings. Always return {@code + * null} before OC. + */ + @Nullable + String getSettingsFragment(); + + boolean isVoicemailArchiveEnabled(Context context, PhoneAccountHandle phoneAccountHandle); + + void setVoicemailArchiveEnabled( + Context context, PhoneAccountHandle phoneAccountHandle, boolean value); +} diff --git a/java/com/android/voicemail/VoicemailComponent.java b/java/com/android/voicemail/VoicemailComponent.java new file mode 100644 index 000000000..6dd6f9d90 --- /dev/null +++ b/java/com/android/voicemail/VoicemailComponent.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.voicemail; + +import android.content.Context; +import dagger.Subcomponent; +import com.android.voicemail.impl.VoicemailClientImpl; + +/** Subcomponent that can be used to access the voicemail implementation. */ +public class VoicemailComponent { + private static VoicemailComponent instance; + private VoicemailClientImpl voicemailClient; + + public VoicemailClient getVoicemailClient() { + if (voicemailClient == null) { + voicemailClient = new VoicemailClientImpl(); + } + return voicemailClient; + } + + public static VoicemailComponent get(Context context) { + if (instance == null) { + instance = new VoicemailComponent(); + } + return instance; + } + + /** Used to refer to the root application component. */ + public interface HasComponent { + VoicemailComponent voicemailComponent(); + } +} diff --git a/java/com/android/voicemail/impl/ActivationTask.java b/java/com/android/voicemail/impl/ActivationTask.java new file mode 100644 index 000000000..c4716116f --- /dev/null +++ b/java/com/android/voicemail/impl/ActivationTask.java @@ -0,0 +1,298 @@ +/* + * 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.voicemail.impl; + +import android.annotation.TargetApi; +import android.content.Context; +import android.content.Intent; +import android.database.ContentObserver; +import android.os.Build.VERSION_CODES; +import android.os.Bundle; +import android.provider.Settings; +import android.provider.Settings.Global; +import android.support.annotation.Nullable; +import android.support.annotation.WorkerThread; +import android.telecom.PhoneAccountHandle; +import android.telephony.ServiceState; +import android.telephony.TelephonyManager; +import com.android.voicemail.impl.protocol.VisualVoicemailProtocol; +import com.android.voicemail.impl.scheduling.BaseTask; +import com.android.voicemail.impl.scheduling.RetryPolicy; +import com.android.voicemail.impl.sms.StatusMessage; +import com.android.voicemail.impl.sms.StatusSmsFetcher; +import com.android.voicemail.impl.sync.OmtpVvmSyncService; +import com.android.voicemail.impl.sync.SyncTask; +import com.android.voicemail.impl.sync.VvmAccountManager; +import java.io.IOException; +import java.util.HashSet; +import java.util.Set; +import java.util.concurrent.CancellationException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeoutException; + +/** + * Task to activate the visual voicemail service. A request to activate VVM will be sent to the + * carrier, which will respond with a STATUS SMS. The credentials will be updated from the SMS. If + * the user is not provisioned provisioning will be attempted. Activation happens when the phone + * boots, the SIM is inserted, signal returned when VVM is not activated yet, and when the carrier + * spontaneously sent a STATUS SMS. + */ +@TargetApi(VERSION_CODES.O) +public class ActivationTask extends BaseTask { + + private static final String TAG = "VvmActivationTask"; + + private static final int RETRY_TIMES = 4; + private static final int RETRY_INTERVAL_MILLIS = 5_000; + + private static final String EXTRA_MESSAGE_DATA_BUNDLE = "extra_message_data_bundle"; + + @Nullable private static DeviceProvisionedObserver sDeviceProvisionedObserver; + + private final RetryPolicy mRetryPolicy; + + private Bundle mMessageData; + + public ActivationTask() { + super(TASK_ACTIVATION); + mRetryPolicy = new RetryPolicy(RETRY_TIMES, RETRY_INTERVAL_MILLIS); + addPolicy(mRetryPolicy); + } + + /** Has the user gone through the setup wizard yet. */ + private static boolean isDeviceProvisioned(Context context) { + return Settings.Global.getInt( + context.getContentResolver(), Settings.Global.DEVICE_PROVISIONED, 0) + == 1; + } + + /** + * @param messageData The optional bundle from {@link android.provider.VoicemailContract# + * EXTRA_VOICEMAIL_SMS_FIELDS}, if the task is initiated by a status SMS. If null the task + * will request a status SMS itself. + */ + public static void start( + Context context, PhoneAccountHandle phoneAccountHandle, @Nullable Bundle messageData) { + if (!isDeviceProvisioned(context)) { + VvmLog.i(TAG, "Activation requested while device is not provisioned, postponing"); + // Activation might need information such as system language to be set, so wait until + // the setup wizard is finished. The data bundle from the SMS will be re-requested upon + // activation. + queueActivationAfterProvisioned(context, phoneAccountHandle); + return; + } + + Intent intent = BaseTask.createIntent(context, ActivationTask.class, phoneAccountHandle); + if (messageData != null) { + intent.putExtra(EXTRA_MESSAGE_DATA_BUNDLE, messageData); + } + context.startService(intent); + } + + @Override + public void onCreate(Context context, Intent intent, int flags, int startId) { + super.onCreate(context, intent, flags, startId); + mMessageData = intent.getParcelableExtra(EXTRA_MESSAGE_DATA_BUNDLE); + } + + @Override + public Intent createRestartIntent() { + Intent intent = super.createRestartIntent(); + // mMessageData is discarded, request a fresh STATUS SMS for retries. + return intent; + } + + @Override + @WorkerThread + public void onExecuteInBackgroundThread() { + Assert.isNotMainThread(); + + PhoneAccountHandle phoneAccountHandle = getPhoneAccountHandle(); + if (phoneAccountHandle == null) { + // This should never happen + VvmLog.e(TAG, "null PhoneAccountHandle"); + return; + } + + OmtpVvmCarrierConfigHelper helper = + new OmtpVvmCarrierConfigHelper(getContext(), phoneAccountHandle); + if (!helper.isValid()) { + VvmLog.i(TAG, "VVM not supported on phoneAccountHandle " + phoneAccountHandle); + VvmAccountManager.removeAccount(getContext(), phoneAccountHandle); + return; + } + + // OmtpVvmCarrierConfigHelper can start the activation process; it will pass in a vvm + // content provider URI which we will use. On some occasions, setting that URI will + // fail, so we will perform a few attempts to ensure that the vvm content provider has + // a good chance of being started up. + if (!VoicemailStatus.edit(getContext(), phoneAccountHandle) + .setType(helper.getVvmType()) + .apply()) { + VvmLog.e(TAG, "Failed to configure content provider - " + helper.getVvmType()); + fail(); + } + VvmLog.i(TAG, "VVM content provider configured - " + helper.getVvmType()); + + if (VvmAccountManager.isAccountActivated(getContext(), phoneAccountHandle)) { + VvmLog.i(TAG, "Account is already activated"); + return; + } + helper.handleEvent( + VoicemailStatus.edit(getContext(), phoneAccountHandle), OmtpEvents.CONFIG_ACTIVATING); + + if (!hasSignal(getContext(), phoneAccountHandle)) { + VvmLog.i(TAG, "Service lost during activation, aborting"); + // Restore the "NO SIGNAL" state since it will be overwritten by the CONFIG_ACTIVATING + // event. + helper.handleEvent( + VoicemailStatus.edit(getContext(), phoneAccountHandle), + OmtpEvents.NOTIFICATION_SERVICE_LOST); + // Don't retry, a new activation will be started after the signal returned. + return; + } + + helper.activateSmsFilter(); + VoicemailStatus.Editor status = mRetryPolicy.getVoicemailStatusEditor(); + + VisualVoicemailProtocol protocol = helper.getProtocol(); + + Bundle data; + if (mMessageData != null) { + // The content of STATUS SMS is provided to launch this task, no need to request it + // again. + data = mMessageData; + } else { + try (StatusSmsFetcher fetcher = new StatusSmsFetcher(getContext(), phoneAccountHandle)) { + protocol.startActivation(helper, fetcher.getSentIntent()); + // Both the fetcher and OmtpMessageReceiver will be triggered, but + // OmtpMessageReceiver will just route the SMS back to ActivationTask, which will be + // rejected because the task is still running. + data = fetcher.get(); + } catch (TimeoutException e) { + // The carrier is expected to return an STATUS SMS within STATUS_SMS_TIMEOUT_MILLIS + // handleEvent() will do the logging. + helper.handleEvent(status, OmtpEvents.CONFIG_STATUS_SMS_TIME_OUT); + fail(); + return; + } catch (CancellationException e) { + VvmLog.e(TAG, "Unable to send status request SMS"); + fail(); + return; + } catch (InterruptedException | ExecutionException | IOException e) { + VvmLog.e(TAG, "can't get future STATUS SMS", e); + fail(); + return; + } + } + + StatusMessage message = new StatusMessage(data); + VvmLog.d( + TAG, + "STATUS SMS received: st=" + + message.getProvisioningStatus() + + ", rc=" + + message.getReturnCode()); + + if (message.getProvisioningStatus().equals(OmtpConstants.SUBSCRIBER_READY)) { + VvmLog.d(TAG, "subscriber ready, no activation required"); + updateSource(getContext(), phoneAccountHandle, status, message); + } else { + if (helper.supportsProvisioning()) { + VvmLog.i(TAG, "Subscriber not ready, start provisioning"); + helper.startProvisioning(this, phoneAccountHandle, status, message, data); + + } else if (message.getProvisioningStatus().equals(OmtpConstants.SUBSCRIBER_NEW)) { + VvmLog.i(TAG, "Subscriber new but provisioning is not supported"); + // Ignore the non-ready state and attempt to use the provided info as is. + // This is probably caused by not completing the new user tutorial. + updateSource(getContext(), phoneAccountHandle, status, message); + } else { + VvmLog.i(TAG, "Subscriber not ready but provisioning is not supported"); + helper.handleEvent(status, OmtpEvents.CONFIG_SERVICE_NOT_AVAILABLE); + } + } + } + + public static void updateSource( + Context context, + PhoneAccountHandle phone, + VoicemailStatus.Editor status, + StatusMessage message) { + + if (OmtpConstants.SUCCESS.equals(message.getReturnCode())) { + OmtpVvmCarrierConfigHelper helper = new OmtpVvmCarrierConfigHelper(context, phone); + helper.handleEvent(status, OmtpEvents.CONFIG_REQUEST_STATUS_SUCCESS); + + // Save the IMAP credentials in preferences so they are persistent and can be retrieved. + VvmAccountManager.addAccount(context, phone, message); + + SyncTask.start(context, phone, OmtpVvmSyncService.SYNC_FULL_SYNC); + } else { + VvmLog.e(TAG, "Visual voicemail not available for subscriber."); + } + } + + private static boolean hasSignal(Context context, PhoneAccountHandle phoneAccountHandle) { + TelephonyManager telephonyManager = + context + .getSystemService(TelephonyManager.class) + .createForPhoneAccountHandle(phoneAccountHandle); + return telephonyManager.getServiceState().getState() == ServiceState.STATE_IN_SERVICE; + } + + private static void queueActivationAfterProvisioned( + Context context, PhoneAccountHandle phoneAccountHandle) { + if (sDeviceProvisionedObserver == null) { + sDeviceProvisionedObserver = new DeviceProvisionedObserver(context); + context + .getContentResolver() + .registerContentObserver( + Settings.Global.getUriFor(Global.DEVICE_PROVISIONED), + false, + sDeviceProvisionedObserver); + } + sDeviceProvisionedObserver.addPhoneAcountHandle(phoneAccountHandle); + } + + private static class DeviceProvisionedObserver extends ContentObserver { + + private final Context mContext; + private final Set mPhoneAccountHandles = new HashSet<>(); + + private DeviceProvisionedObserver(Context context) { + super(null); + mContext = context; + } + + public void addPhoneAcountHandle(PhoneAccountHandle phoneAccountHandle) { + mPhoneAccountHandles.add(phoneAccountHandle); + } + + @Override + public void onChange(boolean selfChange) { + if (isDeviceProvisioned(mContext)) { + VvmLog.i(TAG, "device provisioned, resuming activation"); + for (PhoneAccountHandle phoneAccountHandle : mPhoneAccountHandles) { + start(mContext, phoneAccountHandle, null); + } + mContext.getContentResolver().unregisterContentObserver(sDeviceProvisionedObserver); + sDeviceProvisionedObserver = null; + } + } + } +} diff --git a/java/com/android/voicemail/impl/AndroidManifest.xml b/java/com/android/voicemail/impl/AndroidManifest.xml new file mode 100644 index 000000000..0d90d5932 --- /dev/null +++ b/java/com/android/voicemail/impl/AndroidManifest.xml @@ -0,0 +1,103 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/java/com/android/voicemail/impl/Assert.java b/java/com/android/voicemail/impl/Assert.java new file mode 100644 index 000000000..fe063727a --- /dev/null +++ b/java/com/android/voicemail/impl/Assert.java @@ -0,0 +1,57 @@ +/* + * 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.voicemail.impl; + +import android.os.Looper; + +/** Assertions which will result in program termination. */ +public class Assert { + + private static Boolean sIsMainThreadForTest; + + public static void isTrue(boolean condition) { + if (!condition) { + throw new AssertionError("Expected condition to be true"); + } + } + + public static void isMainThread() { + if (sIsMainThreadForTest != null) { + isTrue(sIsMainThreadForTest); + return; + } + isTrue(Looper.getMainLooper().equals(Looper.myLooper())); + } + + public static void isNotMainThread() { + if (sIsMainThreadForTest != null) { + isTrue(!sIsMainThreadForTest); + return; + } + isTrue(!Looper.getMainLooper().equals(Looper.myLooper())); + } + + public static void fail() { + throw new AssertionError("Fail"); + } + + /** Override the main thread status for tests. Set to null to revert to normal behavior */ + @NeededForTesting + public static void setIsMainThreadForTesting(Boolean isMainThread) { + sIsMainThreadForTest = isMainThread; + } +} diff --git a/java/com/android/voicemail/impl/DefaultOmtpEventHandler.java b/java/com/android/voicemail/impl/DefaultOmtpEventHandler.java new file mode 100644 index 000000000..13aaf0588 --- /dev/null +++ b/java/com/android/voicemail/impl/DefaultOmtpEventHandler.java @@ -0,0 +1,193 @@ +/* + * 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.voicemail.impl; + +import android.content.Context; +import android.provider.VoicemailContract; +import android.provider.VoicemailContract.Status; +import com.android.voicemail.impl.OmtpEvents.Type; + +public class DefaultOmtpEventHandler { + + private static final String TAG = "DefErrorCodeHandler"; + + public static void handleEvent( + Context context, + OmtpVvmCarrierConfigHelper config, + VoicemailStatus.Editor status, + OmtpEvents event) { + switch (event.getType()) { + case Type.CONFIGURATION: + handleConfigurationEvent(context, status, event); + break; + case Type.DATA_CHANNEL: + handleDataChannelEvent(context, status, event); + break; + case Type.NOTIFICATION_CHANNEL: + handleNotificationChannelEvent(context, config, status, event); + break; + case Type.OTHER: + handleOtherEvent(context, status, event); + break; + default: + VvmLog.wtf(TAG, "invalid event type " + event.getType() + " for " + event); + } + } + + private static void handleConfigurationEvent( + Context context, VoicemailStatus.Editor status, OmtpEvents event) { + switch (event) { + case CONFIG_DEFAULT_PIN_REPLACED: + case CONFIG_REQUEST_STATUS_SUCCESS: + case CONFIG_PIN_SET: + status + .setConfigurationState(VoicemailContract.Status.CONFIGURATION_STATE_OK) + .setNotificationChannelState(Status.NOTIFICATION_CHANNEL_STATE_OK) + .apply(); + break; + case CONFIG_ACTIVATING: + // Wipe all errors from the last activation. All errors shown should be new errors + // for this activation. + status + .setConfigurationState(Status.CONFIGURATION_STATE_CONFIGURING) + .setNotificationChannelState(Status.NOTIFICATION_CHANNEL_STATE_OK) + .setDataChannelState(Status.DATA_CHANNEL_STATE_OK) + .apply(); + break; + case CONFIG_ACTIVATING_SUBSEQUENT: + status + .setConfigurationState(Status.CONFIGURATION_STATE_OK) + .setNotificationChannelState(Status.NOTIFICATION_CHANNEL_STATE_OK) + .setDataChannelState(Status.DATA_CHANNEL_STATE_OK) + .apply(); + break; + case CONFIG_SERVICE_NOT_AVAILABLE: + status.setConfigurationState(Status.CONFIGURATION_STATE_FAILED).apply(); + break; + case CONFIG_STATUS_SMS_TIME_OUT: + status.setConfigurationState(Status.CONFIGURATION_STATE_FAILED).apply(); + break; + default: + VvmLog.wtf(TAG, "invalid configuration event " + event); + } + } + + private static void handleDataChannelEvent( + Context context, VoicemailStatus.Editor status, OmtpEvents event) { + switch (event) { + case DATA_IMAP_OPERATION_STARTED: + case DATA_IMAP_OPERATION_COMPLETED: + status.setDataChannelState(Status.DATA_CHANNEL_STATE_OK).apply(); + break; + + case DATA_NO_CONNECTION: + status.setDataChannelState(Status.DATA_CHANNEL_STATE_NO_CONNECTION).apply(); + break; + + case DATA_NO_CONNECTION_CELLULAR_REQUIRED: + status + .setDataChannelState(Status.DATA_CHANNEL_STATE_NO_CONNECTION_CELLULAR_REQUIRED) + .apply(); + break; + case DATA_INVALID_PORT: + status + .setDataChannelState(VoicemailContract.Status.DATA_CHANNEL_STATE_BAD_CONFIGURATION) + .apply(); + break; + case DATA_CANNOT_RESOLVE_HOST_ON_NETWORK: + status + .setDataChannelState( + VoicemailContract.Status.DATA_CHANNEL_STATE_SERVER_CONNECTION_ERROR) + .apply(); + break; + case DATA_SSL_INVALID_HOST_NAME: + case DATA_CANNOT_ESTABLISH_SSL_SESSION: + case DATA_IOE_ON_OPEN: + case DATA_GENERIC_IMAP_IOE: + status + .setDataChannelState(VoicemailContract.Status.DATA_CHANNEL_STATE_COMMUNICATION_ERROR) + .apply(); + break; + case DATA_BAD_IMAP_CREDENTIAL: + case DATA_AUTH_UNKNOWN_USER: + case DATA_AUTH_UNKNOWN_DEVICE: + case DATA_AUTH_INVALID_PASSWORD: + case DATA_AUTH_MAILBOX_NOT_INITIALIZED: + case DATA_AUTH_SERVICE_NOT_PROVISIONED: + case DATA_AUTH_SERVICE_NOT_ACTIVATED: + case DATA_AUTH_USER_IS_BLOCKED: + status + .setDataChannelState(VoicemailContract.Status.DATA_CHANNEL_STATE_BAD_CONFIGURATION) + .apply(); + break; + + case DATA_REJECTED_SERVER_RESPONSE: + case DATA_INVALID_INITIAL_SERVER_RESPONSE: + case DATA_MAILBOX_OPEN_FAILED: + case DATA_SSL_EXCEPTION: + case DATA_ALL_SOCKET_CONNECTION_FAILED: + status + .setDataChannelState(VoicemailContract.Status.DATA_CHANNEL_STATE_SERVER_ERROR) + .apply(); + break; + + default: + VvmLog.wtf(TAG, "invalid data channel event " + event); + } + } + + private static void handleNotificationChannelEvent( + Context context, + OmtpVvmCarrierConfigHelper config, + VoicemailStatus.Editor status, + OmtpEvents event) { + switch (event) { + case NOTIFICATION_IN_SERVICE: + status + .setNotificationChannelState(Status.NOTIFICATION_CHANNEL_STATE_OK) + // Clear the error state. A sync should follow signal return so any error + // will be reposted. + .setDataChannelState(Status.DATA_CHANNEL_STATE_OK) + .apply(); + break; + case NOTIFICATION_SERVICE_LOST: + status.setNotificationChannelState(Status.NOTIFICATION_CHANNEL_STATE_NO_CONNECTION); + if (config.isCellularDataRequired()) { + status.setDataChannelState(Status.DATA_CHANNEL_STATE_NO_CONNECTION_CELLULAR_REQUIRED); + } + status.apply(); + break; + default: + VvmLog.wtf(TAG, "invalid notification channel event " + event); + } + } + + private static void handleOtherEvent( + Context context, VoicemailStatus.Editor status, OmtpEvents event) { + switch (event) { + case OTHER_SOURCE_REMOVED: + status + .setConfigurationState(Status.CONFIGURATION_STATE_NOT_CONFIGURED) + .setNotificationChannelState(Status.NOTIFICATION_CHANNEL_STATE_NO_CONNECTION) + .setDataChannelState(Status.DATA_CHANNEL_STATE_NO_CONNECTION) + .apply(); + break; + default: + VvmLog.wtf(TAG, "invalid other event " + event); + } + } +} diff --git a/java/com/android/voicemail/impl/NeededForTesting.java b/java/com/android/voicemail/impl/NeededForTesting.java new file mode 100644 index 000000000..70e738385 --- /dev/null +++ b/java/com/android/voicemail/impl/NeededForTesting.java @@ -0,0 +1,23 @@ +/* + * 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.voicemail.impl; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +@Retention(RetentionPolicy.SOURCE) +public @interface NeededForTesting {} diff --git a/java/com/android/voicemail/impl/OmtpConstants.java b/java/com/android/voicemail/impl/OmtpConstants.java new file mode 100644 index 000000000..599d0d5f0 --- /dev/null +++ b/java/com/android/voicemail/impl/OmtpConstants.java @@ -0,0 +1,239 @@ +/* + * 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.voicemail.impl; + +import android.support.annotation.IntDef; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +/** + * Wrapper class to hold relevant OMTP constants as defined in the OMTP spec. + * + *

In essence this is a programmatic representation of the relevant portions of OMTP spec. + */ +public class OmtpConstants { + public static final String SMS_FIELD_SEPARATOR = ";"; + public static final String SMS_KEY_VALUE_SEPARATOR = "="; + public static final String SMS_PREFIX_SEPARATOR = ":"; + + public static final String SYNC_SMS_PREFIX = "SYNC"; + public static final String STATUS_SMS_PREFIX = "STATUS"; + + // This is the format designated by the OMTP spec. + public static final String DATE_TIME_FORMAT = "dd/MM/yyyy HH:mm Z"; + + /** OMTP protocol versions. */ + public static final String PROTOCOL_VERSION1_1 = "11"; + + public static final String PROTOCOL_VERSION1_2 = "12"; + public static final String PROTOCOL_VERSION1_3 = "13"; + + ///////////////////////// Client/Mobile originated SMS ////////////////////// + + /** Mobile Originated requests */ + public static final String ACTIVATE_REQUEST = "Activate"; + + public static final String DEACTIVATE_REQUEST = "Deactivate"; + public static final String STATUS_REQUEST = "Status"; + + /** fields that can be present in a Mobile Originated OMTP SMS */ + public static final String CLIENT_TYPE = "ct"; + + public static final String APPLICATION_PORT = "pt"; + public static final String PROTOCOL_VERSION = "pv"; + + //////////////////////////////// Sync SMS fields //////////////////////////// + + /** + * Sync SMS fields. + * + *

Each string constant is the field's key in the SMS body which is used by the parser to + * identify the field's value, if present, in the SMS body. + */ + + /** The event that triggered this SYNC SMS. See {@link OmtpConstants#SYNC_TRIGGER_EVENT_VALUES} */ + public static final String SYNC_TRIGGER_EVENT = "ev"; + + public static final String MESSAGE_UID = "id"; + public static final String MESSAGE_LENGTH = "l"; + public static final String NUM_MESSAGE_COUNT = "c"; + /** See {@link OmtpConstants#CONTENT_TYPE_VALUES} */ + public static final String CONTENT_TYPE = "t"; + + public static final String SENDER = "s"; + public static final String TIME = "dt"; + + /** + * SYNC message trigger events. + * + *

These are the possible values of {@link OmtpConstants#SYNC_TRIGGER_EVENT}. + */ + public static final String NEW_MESSAGE = "NM"; + + public static final String MAILBOX_UPDATE = "MBU"; + public static final String GREETINGS_UPDATE = "GU"; + + public static final String[] SYNC_TRIGGER_EVENT_VALUES = { + NEW_MESSAGE, MAILBOX_UPDATE, GREETINGS_UPDATE + }; + + /** + * Content types supported by OMTP VVM. + * + *

These are the possible values of {@link OmtpConstants#CONTENT_TYPE}. + */ + public static final String VOICE = "v"; + + public static final String VIDEO = "o"; + public static final String FAX = "f"; + /** Voice message deposited by an external application */ + public static final String INFOTAINMENT = "i"; + /** Empty Call Capture - i.e. voicemail with no voice message. */ + public static final String ECC = "e"; + + public static final String[] CONTENT_TYPE_VALUES = {VOICE, VIDEO, FAX, INFOTAINMENT, ECC}; + + ////////////////////////////// Status SMS fields //////////////////////////// + + /** + * Status SMS fields. + * + *

Each string constant is the field's key in the SMS body which is used by the parser to + * identify the field's value, if present, in the SMS body. + */ + /** See {@link OmtpConstants#PROVISIONING_STATUS_VALUES} */ + public static final String PROVISIONING_STATUS = "st"; + /** See {@link OmtpConstants#RETURN_CODE_VALUES} */ + public static final String RETURN_CODE = "rc"; + /** URL to send users to for activation VVM */ + public static final String SUBSCRIPTION_URL = "rs"; + /** IMAP4/SMTP server IP address or fully qualified domain name */ + public static final String SERVER_ADDRESS = "srv"; + /** Phone number to access voicemails through Telephony User Interface */ + public static final String TUI_ACCESS_NUMBER = "tui"; + + public static final String TUI_PASSWORD_LENGTH = "pw_len"; + /** Number to send client origination SMS */ + public static final String CLIENT_SMS_DESTINATION_NUMBER = "dn"; + + public static final String IMAP_PORT = "ipt"; + public static final String IMAP_USER_NAME = "u"; + public static final String IMAP_PASSWORD = "pw"; + public static final String SMTP_PORT = "spt"; + public static final String SMTP_USER_NAME = "smtp_u"; + public static final String SMTP_PASSWORD = "smtp_pw"; + + /** + * User provisioning status values. + * + *

Referred by {@link OmtpConstants#PROVISIONING_STATUS}. + */ + public static final String SUBSCRIBER_NEW = "N"; + + public static final String SUBSCRIBER_READY = "R"; + public static final String SUBSCRIBER_PROVISIONED = "P"; + public static final String SUBSCRIBER_UNKNOWN = "U"; + public static final String SUBSCRIBER_BLOCKED = "B"; + + public static final String[] PROVISIONING_STATUS_VALUES = { + SUBSCRIBER_NEW, SUBSCRIBER_READY, SUBSCRIBER_PROVISIONED, SUBSCRIBER_UNKNOWN, SUBSCRIBER_BLOCKED + }; + + /** + * The return code included in a status message. + * + *

These are the possible values of {@link OmtpConstants#RETURN_CODE}. + */ + public static final String SUCCESS = "0"; + + public static final String SYSTEM_ERROR = "1"; + public static final String SUBSCRIBER_ERROR = "2"; + public static final String MAILBOX_UNKNOWN = "3"; + public static final String VVM_NOT_ACTIVATED = "4"; + public static final String VVM_NOT_PROVISIONED = "5"; + public static final String VVM_CLIENT_UKNOWN = "6"; + public static final String VVM_MAILBOX_NOT_INITIALIZED = "7"; + + public static final String[] RETURN_CODE_VALUES = { + SUCCESS, + SYSTEM_ERROR, + SUBSCRIBER_ERROR, + MAILBOX_UNKNOWN, + VVM_NOT_ACTIVATED, + VVM_NOT_PROVISIONED, + VVM_CLIENT_UKNOWN, + VVM_MAILBOX_NOT_INITIALIZED, + }; + + /** IMAP command extensions */ + + /** + * OMTP spec v1.3 2.3.1 Change password request syntax + * + *

This changes the PIN to access the Telephone User Interface, the traditional voicemail + * system. + */ + public static final String IMAP_CHANGE_TUI_PWD_FORMAT = "XCHANGE_TUI_PWD PWD=%1$s OLD_PWD=%2$s"; + + /** + * OMTP spec v1.3 2.4.1 Change languate request syntax + * + *

This changes the language in the Telephone User Interface. + */ + public static final String IMAP_CHANGE_VM_LANG_FORMAT = "XCHANGE_VM_LANG LANG=%1$s"; + + /** + * OMTP spec v1.3 2.5.1 Close NUT Request syntax + * + *

This disables the new user tutorial, the message played to new users calling in the + * Telephone User Interface. + */ + public static final String IMAP_CLOSE_NUT = "XCLOSE_NUT"; + + /** Possible NO responses for CHANGE_TUI_PWD */ + public static final String RESPONSE_CHANGE_PIN_TOO_SHORT = "password too short"; + + public static final String RESPONSE_CHANGE_PIN_TOO_LONG = "password too long"; + public static final String RESPONSE_CHANGE_PIN_TOO_WEAK = "password too weak"; + public static final String RESPONSE_CHANGE_PIN_MISMATCH = "old password mismatch"; + public static final String RESPONSE_CHANGE_PIN_INVALID_CHARACTER = + "password contains invalid characters"; + + @Retention(RetentionPolicy.SOURCE) + @IntDef( + value = { + CHANGE_PIN_SUCCESS, + CHANGE_PIN_TOO_SHORT, + CHANGE_PIN_TOO_LONG, + CHANGE_PIN_TOO_WEAK, + CHANGE_PIN_MISMATCH, + CHANGE_PIN_INVALID_CHARACTER, + CHANGE_PIN_SYSTEM_ERROR + } + ) + public @interface ChangePinResult {} + + public static final int CHANGE_PIN_SUCCESS = 0; + public static final int CHANGE_PIN_TOO_SHORT = 1; + public static final int CHANGE_PIN_TOO_LONG = 2; + public static final int CHANGE_PIN_TOO_WEAK = 3; + public static final int CHANGE_PIN_MISMATCH = 4; + public static final int CHANGE_PIN_INVALID_CHARACTER = 5; + public static final int CHANGE_PIN_SYSTEM_ERROR = 6; + + /** Indicates the client is Google visual voicemail version 1.0. */ + public static final String CLIENT_TYPE_GOOGLE_10 = "google.vvm.10"; +} diff --git a/java/com/android/voicemail/impl/OmtpEvents.java b/java/com/android/voicemail/impl/OmtpEvents.java new file mode 100644 index 000000000..6807edcf0 --- /dev/null +++ b/java/com/android/voicemail/impl/OmtpEvents.java @@ -0,0 +1,152 @@ +/* + * 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.voicemail.impl; + +import android.support.annotation.IntDef; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +/** + * Events internal to the OMTP client. These should be translated into {@link + * android.provider.VoicemailContract.Status} error codes before writing into the voicemail status + * table. + */ +public enum OmtpEvents { + + // Configuration State + CONFIG_REQUEST_STATUS_SUCCESS(Type.CONFIGURATION, true), + + CONFIG_PIN_SET(Type.CONFIGURATION, true), + // The voicemail PIN is replaced with a generated PIN, user should change it. + CONFIG_DEFAULT_PIN_REPLACED(Type.CONFIGURATION, true), + CONFIG_ACTIVATING(Type.CONFIGURATION, true), + // There are already activation records, this is only a book-keeping activation. + CONFIG_ACTIVATING_SUBSEQUENT(Type.CONFIGURATION, true), + CONFIG_STATUS_SMS_TIME_OUT(Type.CONFIGURATION), + CONFIG_SERVICE_NOT_AVAILABLE(Type.CONFIGURATION), + + // Data channel State + + // A new sync has started, old errors in data channel should be cleared. + DATA_IMAP_OPERATION_STARTED(Type.DATA_CHANNEL, true), + // Successfully downloaded/uploaded data from the server, which means the data channel is clear. + DATA_IMAP_OPERATION_COMPLETED(Type.DATA_CHANNEL, true), + // The port provided in the STATUS SMS is invalid. + DATA_INVALID_PORT(Type.DATA_CHANNEL), + // No connection to the internet, and the carrier requires cellular data + DATA_NO_CONNECTION_CELLULAR_REQUIRED(Type.DATA_CHANNEL), + // No connection to the internet. + DATA_NO_CONNECTION(Type.DATA_CHANNEL), + // Address lookup for the server hostname failed. DNS error? + DATA_CANNOT_RESOLVE_HOST_ON_NETWORK(Type.DATA_CHANNEL), + // All destination address that resolves to the server hostname are rejected or timed out + DATA_ALL_SOCKET_CONNECTION_FAILED(Type.DATA_CHANNEL), + // Failed to establish SSL with the server, either with a direct SSL connection or by + // STARTTLS command + DATA_CANNOT_ESTABLISH_SSL_SESSION(Type.DATA_CHANNEL), + // Identity of the server cannot be verified. + DATA_SSL_INVALID_HOST_NAME(Type.DATA_CHANNEL), + // The server rejected our username/password + DATA_BAD_IMAP_CREDENTIAL(Type.DATA_CHANNEL), + + DATA_AUTH_UNKNOWN_USER(Type.DATA_CHANNEL), + DATA_AUTH_UNKNOWN_DEVICE(Type.DATA_CHANNEL), + DATA_AUTH_INVALID_PASSWORD(Type.DATA_CHANNEL), + DATA_AUTH_MAILBOX_NOT_INITIALIZED(Type.DATA_CHANNEL), + DATA_AUTH_SERVICE_NOT_PROVISIONED(Type.DATA_CHANNEL), + DATA_AUTH_SERVICE_NOT_ACTIVATED(Type.DATA_CHANNEL), + DATA_AUTH_USER_IS_BLOCKED(Type.DATA_CHANNEL), + + // A command to the server didn't result with an "OK" or continuation request + DATA_REJECTED_SERVER_RESPONSE(Type.DATA_CHANNEL), + // The server did not greet us with a "OK", possibly not a IMAP server. + DATA_INVALID_INITIAL_SERVER_RESPONSE(Type.DATA_CHANNEL), + // An IOException occurred while trying to open an ImapConnection + // TODO: reduce scope + DATA_IOE_ON_OPEN(Type.DATA_CHANNEL), + // The SELECT command on a mailbox is rejected + DATA_MAILBOX_OPEN_FAILED(Type.DATA_CHANNEL), + // An IOException has occurred + // TODO: reduce scope + DATA_GENERIC_IMAP_IOE(Type.DATA_CHANNEL), + // An SslException has occurred while opening an ImapConnection + // TODO: reduce scope + DATA_SSL_EXCEPTION(Type.DATA_CHANNEL), + + // Notification Channel + + // Cell signal restored, can received VVM SMSs + NOTIFICATION_IN_SERVICE(Type.NOTIFICATION_CHANNEL, true), + // Cell signal lost, cannot received VVM SMSs + NOTIFICATION_SERVICE_LOST(Type.NOTIFICATION_CHANNEL, false), + + // Other + OTHER_SOURCE_REMOVED(Type.OTHER, false), + + // VVM3 + VVM3_NEW_USER_SETUP_FAILED, + // Table 4. client internal error handling + VVM3_VMG_DNS_FAILURE, + VVM3_SPG_DNS_FAILURE, + VVM3_VMG_CONNECTION_FAILED, + VVM3_SPG_CONNECTION_FAILED, + VVM3_VMG_TIMEOUT, + VVM3_STATUS_SMS_TIMEOUT, + + VVM3_SUBSCRIBER_PROVISIONED, + VVM3_SUBSCRIBER_BLOCKED, + VVM3_SUBSCRIBER_UNKNOWN; + + public static class Type { + + @Retention(RetentionPolicy.SOURCE) + @IntDef({CONFIGURATION, DATA_CHANNEL, NOTIFICATION_CHANNEL, OTHER}) + public @interface Values {} + + public static final int CONFIGURATION = 1; + public static final int DATA_CHANNEL = 2; + public static final int NOTIFICATION_CHANNEL = 3; + public static final int OTHER = 4; + } + + private final int mType; + private final boolean mIsSuccess; + + OmtpEvents(int type, boolean isSuccess) { + mType = type; + mIsSuccess = isSuccess; + } + + OmtpEvents(int type) { + mType = type; + mIsSuccess = false; + } + + OmtpEvents() { + mType = Type.OTHER; + mIsSuccess = false; + } + + @Type.Values + public int getType() { + return mType; + } + + public boolean isSuccess() { + return mIsSuccess; + } +} diff --git a/java/com/android/voicemail/impl/OmtpService.java b/java/com/android/voicemail/impl/OmtpService.java new file mode 100644 index 000000000..dfbd4cf42 --- /dev/null +++ b/java/com/android/voicemail/impl/OmtpService.java @@ -0,0 +1,63 @@ +/* + * 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.voicemail.impl; + +import android.content.Intent; +import android.telecom.PhoneAccountHandle; +import android.telephony.VisualVoicemailService; +import android.telephony.VisualVoicemailSms; +import com.android.voicemail.impl.sync.VvmAccountManager; + +public class OmtpService extends VisualVoicemailService { + + private static final String TAG = "VvmOmtpService"; + + public static final String ACTION_SMS_RECEIVED = "com.android.vociemailomtp.sms.sms_received"; + + public static final String EXTRA_VOICEMAIL_SMS = "extra_voicemail_sms"; + + @Override + public void onCellServiceConnected( + VisualVoicemailTask task, final PhoneAccountHandle phoneAccountHandle) { + VvmLog.i(TAG, "onCellServiceConnected"); + ActivationTask.start(OmtpService.this, phoneAccountHandle, null); + task.finish(); + } + + @Override + public void onSmsReceived(VisualVoicemailTask task, final VisualVoicemailSms sms) { + VvmLog.i(TAG, "onSmsReceived"); + Intent intent = new Intent(ACTION_SMS_RECEIVED); + intent.setPackage(getPackageName()); + intent.putExtra(EXTRA_VOICEMAIL_SMS, sms); + sendBroadcast(intent); + task.finish(); + } + + @Override + public void onSimRemoved( + final VisualVoicemailTask task, final PhoneAccountHandle phoneAccountHandle) { + VvmLog.i(TAG, "onSimRemoved"); + VvmAccountManager.removeAccount(this, phoneAccountHandle); + task.finish(); + } + + @Override + public void onStopped(VisualVoicemailTask task) { + VvmLog.i(TAG, "onStopped"); + } +} diff --git a/java/com/android/voicemail/impl/OmtpVvmCarrierConfigHelper.java b/java/com/android/voicemail/impl/OmtpVvmCarrierConfigHelper.java new file mode 100644 index 000000000..0296d208d --- /dev/null +++ b/java/com/android/voicemail/impl/OmtpVvmCarrierConfigHelper.java @@ -0,0 +1,444 @@ +/* + * 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.voicemail.impl; + +import android.app.PendingIntent; +import android.content.Context; +import android.content.pm.PackageManager.NameNotFoundException; +import android.os.Bundle; +import android.os.PersistableBundle; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.support.annotation.VisibleForTesting; +import android.telecom.PhoneAccountHandle; +import android.telephony.CarrierConfigManager; +import android.telephony.TelephonyManager; +import android.telephony.VisualVoicemailService; +import android.telephony.VisualVoicemailSmsFilterSettings; +import android.text.TextUtils; +import android.util.ArraySet; +import com.android.dialer.common.Assert; +import com.android.voicemail.impl.protocol.VisualVoicemailProtocol; +import com.android.voicemail.impl.protocol.VisualVoicemailProtocolFactory; +import com.android.voicemail.impl.sms.StatusMessage; +import com.android.voicemail.impl.sync.VvmAccountManager; +import java.util.Collections; +import java.util.Set; + +/** + * Manages carrier dependent visual voicemail configuration values. The primary source is the value + * retrieved from CarrierConfigManager. If CarrierConfigManager does not provide the config + * (KEY_VVM_TYPE_STRING is empty, or "hidden" configs), then the value hardcoded in telephony will + * be used (in res/xml/vvm_config.xml) + * + *

Hidden configs are new configs that are planned for future APIs, or miscellaneous settings + * that may clutter CarrierConfigManager too much. + * + *

The current hidden configs are: {@link #getSslPort()} {@link #getDisabledCapabilities()} + */ +public class OmtpVvmCarrierConfigHelper { + + private static final String TAG = "OmtpVvmCarrierCfgHlpr"; + + static final String KEY_VVM_TYPE_STRING = CarrierConfigManager.KEY_VVM_TYPE_STRING; + static final String KEY_VVM_DESTINATION_NUMBER_STRING = + CarrierConfigManager.KEY_VVM_DESTINATION_NUMBER_STRING; + static final String KEY_VVM_PORT_NUMBER_INT = CarrierConfigManager.KEY_VVM_PORT_NUMBER_INT; + static final String KEY_CARRIER_VVM_PACKAGE_NAME_STRING = + CarrierConfigManager.KEY_CARRIER_VVM_PACKAGE_NAME_STRING; + static final String KEY_CARRIER_VVM_PACKAGE_NAME_STRING_ARRAY = + "carrier_vvm_package_name_string_array"; + static final String KEY_VVM_PREFETCH_BOOL = CarrierConfigManager.KEY_VVM_PREFETCH_BOOL; + static final String KEY_VVM_CELLULAR_DATA_REQUIRED_BOOL = + CarrierConfigManager.KEY_VVM_CELLULAR_DATA_REQUIRED_BOOL; + + /** @see #getSslPort() */ + static final String KEY_VVM_SSL_PORT_NUMBER_INT = "vvm_ssl_port_number_int"; + + /** @see #isLegacyModeEnabled() */ + static final String KEY_VVM_LEGACY_MODE_ENABLED_BOOL = "vvm_legacy_mode_enabled_bool"; + + /** + * Ban a capability reported by the server from being used. The array of string should be a subset + * of the capabilities returned IMAP CAPABILITY command. + * + * @see #getDisabledCapabilities() + */ + static final String KEY_VVM_DISABLED_CAPABILITIES_STRING_ARRAY = + "vvm_disabled_capabilities_string_array"; + + static final String KEY_VVM_CLIENT_PREFIX_STRING = "vvm_client_prefix_string"; + + private final Context mContext; + private final PersistableBundle mCarrierConfig; + private final String mVvmType; + private final VisualVoicemailProtocol mProtocol; + private final PersistableBundle mTelephonyConfig; + + private PhoneAccountHandle mPhoneAccountHandle; + + public OmtpVvmCarrierConfigHelper(Context context, PhoneAccountHandle handle) { + mContext = context; + mPhoneAccountHandle = handle; + TelephonyManager telephonyManager = + context + .getSystemService(TelephonyManager.class) + .createForPhoneAccountHandle(mPhoneAccountHandle); + if (telephonyManager == null) { + VvmLog.e(TAG, "PhoneAccountHandle is invalid"); + mCarrierConfig = null; + mTelephonyConfig = null; + mVvmType = null; + mProtocol = null; + return; + } + + mCarrierConfig = getCarrierConfig(telephonyManager); + mTelephonyConfig = + new TelephonyVvmConfigManager(context.getResources()) + .getConfig(telephonyManager.getSimOperator()); + + mVvmType = getVvmType(); + mProtocol = VisualVoicemailProtocolFactory.create(mContext.getResources(), mVvmType); + } + + @VisibleForTesting + OmtpVvmCarrierConfigHelper( + Context context, PersistableBundle carrierConfig, PersistableBundle telephonyConfig) { + mContext = context; + mCarrierConfig = carrierConfig; + mTelephonyConfig = telephonyConfig; + mVvmType = getVvmType(); + mProtocol = VisualVoicemailProtocolFactory.create(mContext.getResources(), mVvmType); + } + + public Context getContext() { + return mContext; + } + + @Nullable + public PhoneAccountHandle getPhoneAccountHandle() { + return mPhoneAccountHandle; + } + + /** + * return whether the carrier's visual voicemail is supported, with KEY_VVM_TYPE_STRING set as a + * known protocol. + */ + public boolean isValid() { + return mProtocol != null; + } + + @Nullable + public String getVvmType() { + return (String) getValue(KEY_VVM_TYPE_STRING); + } + + @Nullable + public VisualVoicemailProtocol getProtocol() { + return mProtocol; + } + + /** @returns arbitrary String stored in the config file. Used for protocol specific values. */ + @Nullable + public String getString(String key) { + Assert.checkArgument(isValid()); + return (String) getValue(key); + } + + @Nullable + public Set getCarrierVvmPackageNames() { + Assert.checkArgument(isValid()); + Set names = getCarrierVvmPackageNames(mCarrierConfig); + if (names != null) { + return names; + } + return getCarrierVvmPackageNames(mTelephonyConfig); + } + + private static Set getCarrierVvmPackageNames(@Nullable PersistableBundle bundle) { + if (bundle == null) { + return null; + } + Set names = new ArraySet<>(); + if (bundle.containsKey(KEY_CARRIER_VVM_PACKAGE_NAME_STRING)) { + names.add(bundle.getString(KEY_CARRIER_VVM_PACKAGE_NAME_STRING)); + } + if (bundle.containsKey(KEY_CARRIER_VVM_PACKAGE_NAME_STRING_ARRAY)) { + String[] vvmPackages = bundle.getStringArray(KEY_CARRIER_VVM_PACKAGE_NAME_STRING_ARRAY); + if (vvmPackages != null && vvmPackages.length > 0) { + Collections.addAll(names, vvmPackages); + } + } + if (names.isEmpty()) { + return null; + } + return names; + } + + /** + * For checking upon sim insertion whether visual voicemail should be enabled. This method does so + * by checking if the carrier's voicemail app is installed. + */ + public boolean isEnabledByDefault() { + if (!isValid()) { + return false; + } + + Set carrierPackages = getCarrierVvmPackageNames(); + if (carrierPackages == null) { + return true; + } + for (String packageName : carrierPackages) { + try { + mContext.getPackageManager().getPackageInfo(packageName, 0); + return false; + } catch (NameNotFoundException e) { + // Do nothing. + } + } + return true; + } + + public boolean isCellularDataRequired() { + Assert.checkArgument(isValid()); + return (boolean) getValue(KEY_VVM_CELLULAR_DATA_REQUIRED_BOOL, false); + } + + public boolean isPrefetchEnabled() { + Assert.checkArgument(isValid()); + return (boolean) getValue(KEY_VVM_PREFETCH_BOOL, true); + } + + public int getApplicationPort() { + Assert.checkArgument(isValid()); + return (int) getValue(KEY_VVM_PORT_NUMBER_INT, 0); + } + + @Nullable + public String getDestinationNumber() { + Assert.checkArgument(isValid()); + return (String) getValue(KEY_VVM_DESTINATION_NUMBER_STRING); + } + + /** + * @return Port to start a SSL IMAP connection directly. + */ + public int getSslPort() { + Assert.checkArgument(isValid()); + return (int) getValue(KEY_VVM_SSL_PORT_NUMBER_INT, 0); + } + + /** + * Hidden Config. + * + *

Sometimes the server states it supports a certain feature but we found they have bug on the + * server side. For example, in b/28717550 the server reported AUTH=DIGEST-MD5 capability but + * using it to login will cause subsequent response to be erroneous. + * + * @return A set of capabilities that is reported by the IMAP CAPABILITY command, but determined + * to have issues and should not be used. + */ + @Nullable + public Set getDisabledCapabilities() { + Assert.checkArgument(isValid()); + Set disabledCapabilities = getDisabledCapabilities(mCarrierConfig); + if (disabledCapabilities != null) { + return disabledCapabilities; + } + return getDisabledCapabilities(mTelephonyConfig); + } + + @Nullable + private static Set getDisabledCapabilities(@Nullable PersistableBundle bundle) { + if (bundle == null) { + return null; + } + if (!bundle.containsKey(KEY_VVM_DISABLED_CAPABILITIES_STRING_ARRAY)) { + return null; + } + String[] disabledCapabilities = + bundle.getStringArray(KEY_VVM_DISABLED_CAPABILITIES_STRING_ARRAY); + if (disabledCapabilities != null && disabledCapabilities.length > 0) { + ArraySet result = new ArraySet<>(); + Collections.addAll(result, disabledCapabilities); + return result; + } + return null; + } + + public String getClientPrefix() { + Assert.checkArgument(isValid()); + String prefix = (String) getValue(KEY_VVM_CLIENT_PREFIX_STRING); + if (prefix != null) { + return prefix; + } + return "//VVM"; + } + + /** + * Should legacy mode be used when the OMTP VVM client is disabled? + * + *

Legacy mode is a mode that on the carrier side visual voicemail is still activated, but on + * the client side all network operations are disabled. SMSs are still monitored so a new message + * SYNC SMS will be translated to show a message waiting indicator, like traditional voicemails. + * + *

This is for carriers that does not support VVM deactivation so voicemail can continue to + * function without the data cost. + */ + public boolean isLegacyModeEnabled() { + Assert.checkArgument(isValid()); + return (boolean) getValue(KEY_VVM_LEGACY_MODE_ENABLED_BOOL, false); + } + + public void startActivation() { + Assert.checkArgument(isValid()); + PhoneAccountHandle phoneAccountHandle = getPhoneAccountHandle(); + if (phoneAccountHandle == null) { + // This should never happen + // Error logged in getPhoneAccountHandle(). + return; + } + + if (mVvmType == null || mVvmType.isEmpty()) { + // The VVM type is invalid; we should never have gotten here in the first place since + // this is loaded initially in the constructor, and callers should check isValid() + // before trying to start activation anyways. + VvmLog.e(TAG, "startActivation : vvmType is null or empty for account " + phoneAccountHandle); + return; + } + + if (mProtocol != null) { + ActivationTask.start(mContext, mPhoneAccountHandle, null); + } + } + + public void activateSmsFilter() { + Assert.checkArgument(isValid()); + VisualVoicemailService.setSmsFilterSettings( + mContext, + getPhoneAccountHandle(), + new VisualVoicemailSmsFilterSettings.Builder().setClientPrefix(getClientPrefix()).build()); + } + + public void startDeactivation() { + Assert.checkArgument(isValid()); + if (!isLegacyModeEnabled()) { + // SMS should still be filtered in legacy mode + VisualVoicemailService.setSmsFilterSettings(mContext, getPhoneAccountHandle(), null); + } + if (mProtocol != null) { + mProtocol.startDeactivation(this); + } + VvmAccountManager.removeAccount(mContext, getPhoneAccountHandle()); + } + + public boolean supportsProvisioning() { + Assert.checkArgument(isValid()); + return mProtocol.supportsProvisioning(); + } + + public void startProvisioning( + ActivationTask task, + PhoneAccountHandle phone, + VoicemailStatus.Editor status, + StatusMessage message, + Bundle data) { + Assert.checkArgument(isValid()); + mProtocol.startProvisioning(task, phone, this, status, message, data); + } + + public void requestStatus(@Nullable PendingIntent sentIntent) { + Assert.checkArgument(isValid()); + mProtocol.requestStatus(this, sentIntent); + } + + public void handleEvent(VoicemailStatus.Editor status, OmtpEvents event) { + Assert.checkArgument(isValid()); + VvmLog.i(TAG, "OmtpEvent:" + event); + mProtocol.handleEvent(mContext, this, status, event); + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder("OmtpVvmCarrierConfigHelper ["); + builder + .append("phoneAccountHandle: ") + .append(mPhoneAccountHandle) + .append(", carrierConfig: ") + .append(mCarrierConfig != null) + .append(", telephonyConfig: ") + .append(mTelephonyConfig != null) + .append(", type: ") + .append(getVvmType()) + .append(", destinationNumber: ") + .append(getDestinationNumber()) + .append(", applicationPort: ") + .append(getApplicationPort()) + .append(", sslPort: ") + .append(getSslPort()) + .append(", isEnabledByDefault: ") + .append(isEnabledByDefault()) + .append(", isCellularDataRequired: ") + .append(isCellularDataRequired()) + .append(", isPrefetchEnabled: ") + .append(isPrefetchEnabled()) + .append(", isLegacyModeEnabled: ") + .append(isLegacyModeEnabled()) + .append("]"); + return builder.toString(); + } + + @Nullable + private PersistableBundle getCarrierConfig(@NonNull TelephonyManager telephonyManager) { + CarrierConfigManager carrierConfigManager = + (CarrierConfigManager) mContext.getSystemService(Context.CARRIER_CONFIG_SERVICE); + if (carrierConfigManager == null) { + VvmLog.w(TAG, "No carrier config service found."); + return null; + } + + PersistableBundle config = telephonyManager.getCarrierConfig(); + + if (TextUtils.isEmpty(config.getString(CarrierConfigManager.KEY_VVM_TYPE_STRING))) { + return null; + } + return config; + } + + @Nullable + private Object getValue(String key) { + return getValue(key, null); + } + + @Nullable + private Object getValue(String key, Object defaultValue) { + Object result; + if (mCarrierConfig != null) { + result = mCarrierConfig.get(key); + if (result != null) { + return result; + } + } + if (mTelephonyConfig != null) { + result = mTelephonyConfig.get(key); + if (result != null) { + return result; + } + } + return defaultValue; + } +} diff --git a/java/com/android/voicemail/impl/SubscriptionInfoHelper.java b/java/com/android/voicemail/impl/SubscriptionInfoHelper.java new file mode 100644 index 000000000..d8a8423eb --- /dev/null +++ b/java/com/android/voicemail/impl/SubscriptionInfoHelper.java @@ -0,0 +1,70 @@ +/** + * Copyright (C) 2014 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.voicemail.impl; + +import android.app.ActionBar; +import android.content.Context; +import android.content.Intent; +import android.content.res.Resources; +import android.text.TextUtils; + +/** + * Helper for manipulating intents or components with subscription-related information. + * + *

In settings, subscription ids and labels are passed along to indicate that settings are being + * changed for particular subscriptions. This helper provides functions for helping extract this + * info and perform common operations using this info. + */ +public class SubscriptionInfoHelper { + public static final int NO_SUB_ID = -1; + + // Extra on intent containing the id of a subscription. + public static final String SUB_ID_EXTRA = + "com.android.voicemailomtp.settings.SubscriptionInfoHelper.SubscriptionId"; + // Extra on intent containing the label of a subscription. + private static final String SUB_LABEL_EXTRA = + "com.android.voicemailomtp.settings.SubscriptionInfoHelper.SubscriptionLabel"; + + private static Context mContext; + + private static int mSubId = NO_SUB_ID; + private static String mSubLabel; + + /** Instantiates the helper, by extracting the subscription id and label from the intent. */ + public SubscriptionInfoHelper(Context context, Intent intent) { + mContext = context; + mSubId = intent.getIntExtra(SUB_ID_EXTRA, NO_SUB_ID); + mSubLabel = intent.getStringExtra(SUB_LABEL_EXTRA); + } + + /** + * Sets the action bar title to the string specified by the given resource id, formatting it with + * the subscription label. This assumes the resource string is formattable with a string-type + * specifier. + * + *

If the subscription label does not exists, leave the existing title. + */ + public void setActionBarTitle(ActionBar actionBar, Resources res, int resId) { + if (actionBar == null || TextUtils.isEmpty(mSubLabel)) { + return; + } + + String title = String.format(res.getString(resId), mSubLabel); + actionBar.setTitle(title); + } + + public int getSubId() { + return mSubId; + } +} diff --git a/java/com/android/voicemail/impl/TelephonyManagerStub.java b/java/com/android/voicemail/impl/TelephonyManagerStub.java new file mode 100644 index 000000000..4762e9023 --- /dev/null +++ b/java/com/android/voicemail/impl/TelephonyManagerStub.java @@ -0,0 +1,40 @@ +/* + * 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.voicemail.impl; + +import android.annotation.TargetApi; +import android.os.Build.VERSION_CODES; + +/** + * Temporary stub for public APIs that should be added into telephony manager. + * + *

TODO(b/32637799) remove this. + */ +@TargetApi(VERSION_CODES.O) +public class TelephonyManagerStub { + + public static void showVoicemailNotification(int voicemailCount) {} + + /** + * Dismisses the message waiting (voicemail) indicator. + * + * @param subId the subscription id we should dismiss the notification for. + */ + public static void clearMwiIndicator(int subId) {} + + public static void setShouldCheckVisualVoicemailConfigurationForMwi(int subId, boolean enabled) {} +} diff --git a/java/com/android/voicemail/impl/TelephonyMangerCompat.java b/java/com/android/voicemail/impl/TelephonyMangerCompat.java new file mode 100644 index 000000000..353cd69e3 --- /dev/null +++ b/java/com/android/voicemail/impl/TelephonyMangerCompat.java @@ -0,0 +1,57 @@ +/* + * 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.voicemail.impl; + +import android.telecom.PhoneAccountHandle; +import android.telephony.TelephonyManager; +import java.lang.reflect.Method; + +/** Handles {@link TelephonyManager} API changes in experimental SDK */ +public class TelephonyMangerCompat { + + private static final String GET_VISUAL_VOICEMAIL_PACKGE_NAME = "getVisualVoicemailPackageName"; + + /** + * Changed from getVisualVoicemailPackageName(PhoneAccountHandle) to + * getVisualVoicemailPackageName() + */ + public static String getVisualVoicemailPackageName(TelephonyManager telephonyManager) { + try { + Method method = TelephonyManager.class.getMethod(GET_VISUAL_VOICEMAIL_PACKGE_NAME); + try { + return (String) method.invoke(telephonyManager); + } catch (ReflectiveOperationException e) { + throw new RuntimeException(e); + } + } catch (NoSuchMethodException e) { + // Do nothing, try the next version. + } + + try { + Method method = + TelephonyManager.class.getMethod( + GET_VISUAL_VOICEMAIL_PACKGE_NAME, PhoneAccountHandle.class); + try { + return (String) method.invoke(telephonyManager, (Object) null); + } catch (ReflectiveOperationException e) { + throw new RuntimeException(e); + } + } catch (NoSuchMethodException e) { + throw new RuntimeException(e); + } + } +} diff --git a/java/com/android/voicemail/impl/TelephonyVvmConfigManager.java b/java/com/android/voicemail/impl/TelephonyVvmConfigManager.java new file mode 100644 index 000000000..04012c9c2 --- /dev/null +++ b/java/com/android/voicemail/impl/TelephonyVvmConfigManager.java @@ -0,0 +1,150 @@ +/* + * 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.voicemail.impl; + +import android.content.res.Resources; +import android.os.PersistableBundle; +import android.support.annotation.Nullable; +import android.support.annotation.VisibleForTesting; +import android.util.ArrayMap; +import com.android.voicemail.impl.utils.XmlUtils; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Map; +import java.util.Map.Entry; +import org.xmlpull.v1.XmlPullParser; +import org.xmlpull.v1.XmlPullParserException; + +/** Load and caches telephony vvm config from res/xml/vvm_config.xml */ +public class TelephonyVvmConfigManager { + + private static final String TAG = "TelephonyVvmCfgMgr"; + + private static final boolean USE_DEBUG_CONFIG = false; + + private static final String TAG_PERSISTABLEMAP = "pbundle_as_map"; + + static final String KEY_MCCMNC = "mccmnc"; + + private static Map sCachedConfigs; + + private final Map mConfigs; + + public TelephonyVvmConfigManager(Resources resources) { + if (sCachedConfigs == null) { + sCachedConfigs = loadConfigs(resources.getXml(R.xml.vvm_config)); + } + mConfigs = sCachedConfigs; + } + + @VisibleForTesting + TelephonyVvmConfigManager(XmlPullParser parser) { + mConfigs = loadConfigs(parser); + } + + @Nullable + public PersistableBundle getConfig(String mccMnc) { + if (USE_DEBUG_CONFIG) { + return mConfigs.get("TEST"); + } + return mConfigs.get(mccMnc); + } + + private static Map loadConfigs(XmlPullParser parser) { + Map configs = new ArrayMap<>(); + try { + ArrayList list = readBundleList(parser); + for (Object object : list) { + if (!(object instanceof PersistableBundle)) { + throw new IllegalArgumentException("PersistableBundle expected, got " + object); + } + PersistableBundle bundle = (PersistableBundle) object; + String[] mccMncs = bundle.getStringArray(KEY_MCCMNC); + if (mccMncs == null) { + throw new IllegalArgumentException("MCCMNC is null"); + } + for (String mccMnc : mccMncs) { + configs.put(mccMnc, bundle); + } + } + } catch (IOException | XmlPullParserException e) { + throw new RuntimeException(e); + } + return configs; + } + + @Nullable + public static ArrayList readBundleList(XmlPullParser in) + throws IOException, XmlPullParserException { + final int outerDepth = in.getDepth(); + int event; + while (((event = in.next()) != XmlPullParser.END_DOCUMENT) + && (event != XmlPullParser.END_TAG || in.getDepth() < outerDepth)) { + if (event == XmlPullParser.START_TAG) { + final String startTag = in.getName(); + final String[] tagName = new String[1]; + in.next(); + return XmlUtils.readThisListXml(in, startTag, tagName, new MyReadMapCallback(), false); + } + } + return null; + } + + public static PersistableBundle restoreFromXml(XmlPullParser in) + throws IOException, XmlPullParserException { + final int outerDepth = in.getDepth(); + final String startTag = in.getName(); + final String[] tagName = new String[1]; + int event; + while (((event = in.next()) != XmlPullParser.END_DOCUMENT) + && (event != XmlPullParser.END_TAG || in.getDepth() < outerDepth)) { + if (event == XmlPullParser.START_TAG) { + ArrayMap map = + XmlUtils.readThisArrayMapXml(in, startTag, tagName, new MyReadMapCallback()); + PersistableBundle result = new PersistableBundle(); + for (Entry entry : map.entrySet()) { + Object value = entry.getValue(); + if (value instanceof Integer) { + result.putInt(entry.getKey(), (int) value); + } else if (value instanceof Boolean) { + result.putBoolean(entry.getKey(), (boolean) value); + } else if (value instanceof String) { + result.putString(entry.getKey(), (String) value); + } else if (value instanceof String[]) { + result.putStringArray(entry.getKey(), (String[]) value); + } else if (value instanceof PersistableBundle) { + result.putPersistableBundle(entry.getKey(), (PersistableBundle) value); + } + } + return result; + } + } + return PersistableBundle.EMPTY; + } + + static class MyReadMapCallback implements XmlUtils.ReadMapCallback { + + @Override + public Object readThisUnknownObjectXml(XmlPullParser in, String tag) + throws XmlPullParserException, IOException { + if (TAG_PERSISTABLEMAP.equals(tag)) { + return restoreFromXml(in); + } + throw new XmlPullParserException("Unknown tag=" + tag); + } + } +} diff --git a/java/com/android/voicemail/impl/VisualVoicemailPreferences.java b/java/com/android/voicemail/impl/VisualVoicemailPreferences.java new file mode 100644 index 000000000..72506eb93 --- /dev/null +++ b/java/com/android/voicemail/impl/VisualVoicemailPreferences.java @@ -0,0 +1,37 @@ +/* + * 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.voicemail.impl; + +import android.content.Context; +import android.preference.PreferenceManager; +import android.telecom.PhoneAccountHandle; +import com.android.dialer.common.PerAccountSharedPreferences; + +/** + * Save visual voicemail values in shared preferences to be retrieved later. Because a voicemail + * source is tied 1:1 to a phone account, the phone account handle is used in the key for each + * voicemail source and the associated data. + */ +public class VisualVoicemailPreferences extends PerAccountSharedPreferences { + + public VisualVoicemailPreferences(Context context, PhoneAccountHandle phoneAccountHandle) { + super( + context, + phoneAccountHandle, + PreferenceManager.getDefaultSharedPreferences(context), + "visual_voicemail_"); + } +} diff --git a/java/com/android/voicemail/impl/Voicemail.java b/java/com/android/voicemail/impl/Voicemail.java new file mode 100644 index 000000000..f98d56f0a --- /dev/null +++ b/java/com/android/voicemail/impl/Voicemail.java @@ -0,0 +1,341 @@ +/* + * 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.voicemail.impl; + +import android.net.Uri; +import android.os.Parcel; +import android.os.Parcelable; +import android.telecom.PhoneAccountHandle; +import android.text.TextUtils; + +/** Represents a single voicemail stored in the voicemail content provider. */ +public class Voicemail implements Parcelable { + + private final Long mTimestamp; + private final String mNumber; + private final PhoneAccountHandle mPhoneAccount; + private final Long mId; + private final Long mDuration; + private final String mSource; + private final String mProviderData; + private final Uri mUri; + private final Boolean mIsRead; + private final Boolean mHasContent; + private final String mTranscription; + + private Voicemail( + Long timestamp, + String number, + PhoneAccountHandle phoneAccountHandle, + Long id, + Long duration, + String source, + String providerData, + Uri uri, + Boolean isRead, + Boolean hasContent, + String transcription) { + mTimestamp = timestamp; + mNumber = number; + mPhoneAccount = phoneAccountHandle; + mId = id; + mDuration = duration; + mSource = source; + mProviderData = providerData; + mUri = uri; + mIsRead = isRead; + mHasContent = hasContent; + mTranscription = transcription; + } + + /** + * Create a {@link Builder} for a new {@link Voicemail} to be inserted. + * + *

The number and the timestamp are mandatory for insertion. + */ + public static Builder createForInsertion(long timestamp, String number) { + return new Builder().setNumber(number).setTimestamp(timestamp); + } + + /** + * Create a {@link Builder} for a {@link Voicemail} to be updated (or deleted). + * + *

The id and source data fields are mandatory for update - id is necessary for updating the + * database and source data is necessary for updating the server. + */ + public static Builder createForUpdate(long id, String sourceData) { + return new Builder().setId(id).setSourceData(sourceData); + } + + /** + * Builder pattern for creating a {@link Voicemail}. The builder must be created with the {@link + * #createForInsertion(long, String)} method. + * + *

This class is not thread safe + */ + public static class Builder { + + private Long mBuilderTimestamp; + private String mBuilderNumber; + private PhoneAccountHandle mBuilderPhoneAccount; + private Long mBuilderId; + private Long mBuilderDuration; + private String mBuilderSourcePackage; + private String mBuilderSourceData; + private Uri mBuilderUri; + private Boolean mBuilderIsRead; + private boolean mBuilderHasContent; + private String mBuilderTranscription; + + /** You should use the correct factory method to construct a builder. */ + private Builder() {} + + public Builder setNumber(String number) { + mBuilderNumber = number; + return this; + } + + public Builder setTimestamp(long timestamp) { + mBuilderTimestamp = timestamp; + return this; + } + + public Builder setPhoneAccount(PhoneAccountHandle phoneAccount) { + mBuilderPhoneAccount = phoneAccount; + return this; + } + + public Builder setId(long id) { + mBuilderId = id; + return this; + } + + public Builder setDuration(long duration) { + mBuilderDuration = duration; + return this; + } + + public Builder setSourcePackage(String sourcePackage) { + mBuilderSourcePackage = sourcePackage; + return this; + } + + public Builder setSourceData(String sourceData) { + mBuilderSourceData = sourceData; + return this; + } + + public Builder setUri(Uri uri) { + mBuilderUri = uri; + return this; + } + + public Builder setIsRead(boolean isRead) { + mBuilderIsRead = isRead; + return this; + } + + public Builder setHasContent(boolean hasContent) { + mBuilderHasContent = hasContent; + return this; + } + + public Builder setTranscription(String transcription) { + mBuilderTranscription = transcription; + return this; + } + + public Voicemail build() { + mBuilderId = mBuilderId == null ? -1 : mBuilderId; + mBuilderTimestamp = mBuilderTimestamp == null ? 0 : mBuilderTimestamp; + mBuilderDuration = mBuilderDuration == null ? 0 : mBuilderDuration; + mBuilderIsRead = mBuilderIsRead == null ? false : mBuilderIsRead; + return new Voicemail( + mBuilderTimestamp, + mBuilderNumber, + mBuilderPhoneAccount, + mBuilderId, + mBuilderDuration, + mBuilderSourcePackage, + mBuilderSourceData, + mBuilderUri, + mBuilderIsRead, + mBuilderHasContent, + mBuilderTranscription); + } + } + + /** + * The identifier of the voicemail in the content provider. + * + *

This may be missing in the case of a new {@link Voicemail} that we plan to insert into the + * content provider, since until it has been inserted we don't know what id it should have. If + * none is specified, we return -1. + */ + public long getId() { + return mId; + } + + /** The number of the person leaving the voicemail, empty string if unknown, null if not set. */ + public String getNumber() { + return mNumber; + } + + /** The phone account associated with the voicemail, null if not set. */ + public PhoneAccountHandle getPhoneAccount() { + return mPhoneAccount; + } + + /** The timestamp the voicemail was received, in millis since the epoch, zero if not set. */ + public long getTimestampMillis() { + return mTimestamp; + } + + /** Gets the duration of the voicemail in millis, or zero if the field is not set. */ + public long getDuration() { + return mDuration; + } + + /** + * Returns the package name of the source that added this voicemail, or null if this field is not + * set. + */ + public String getSourcePackage() { + return mSource; + } + + /** + * Returns the application-specific data type stored with the voicemail, or null if this field is + * not set. + * + *

Source data is typically used as an identifier to uniquely identify the voicemail against + * the voicemail server. This is likely to be something like the IMAP UID, or some other + * server-generated identifying string. + */ + public String getSourceData() { + return mProviderData; + } + + /** + * Gets the Uri that can be used to refer to this voicemail, and to make it play. + * + *

Returns null if we don't know the Uri. + */ + public Uri getUri() { + return mUri; + } + + /** + * Tells us if the voicemail message has been marked as read. + * + *

Always returns false if this field has not been set, i.e. if hasRead() returns false. + */ + public boolean isRead() { + return mIsRead; + } + + /** Tells us if there is content stored at the Uri. */ + public boolean hasContent() { + return mHasContent; + } + + /** Returns the text transcription of this voicemail, or null if this field is not set. */ + public String getTranscription() { + return mTranscription; + } + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(Parcel dest, int flags) { + dest.writeLong(mTimestamp); + writeCharSequence(dest, mNumber); + if (mPhoneAccount == null) { + dest.writeInt(0); + } else { + dest.writeInt(1); + mPhoneAccount.writeToParcel(dest, flags); + } + dest.writeLong(mId); + dest.writeLong(mDuration); + writeCharSequence(dest, mSource); + writeCharSequence(dest, mProviderData); + if (mUri == null) { + dest.writeInt(0); + } else { + dest.writeInt(1); + mUri.writeToParcel(dest, flags); + } + if (mIsRead) { + dest.writeInt(1); + } else { + dest.writeInt(0); + } + if (mHasContent) { + dest.writeInt(1); + } else { + dest.writeInt(0); + } + writeCharSequence(dest, mTranscription); + } + + public static final Creator CREATOR = + new Creator() { + @Override + public Voicemail createFromParcel(Parcel in) { + return new Voicemail(in); + } + + @Override + public Voicemail[] newArray(int size) { + return new Voicemail[size]; + } + }; + + private Voicemail(Parcel in) { + mTimestamp = in.readLong(); + mNumber = (String) readCharSequence(in); + if (in.readInt() > 0) { + mPhoneAccount = PhoneAccountHandle.CREATOR.createFromParcel(in); + } else { + mPhoneAccount = null; + } + mId = in.readLong(); + mDuration = in.readLong(); + mSource = (String) readCharSequence(in); + mProviderData = (String) readCharSequence(in); + if (in.readInt() > 0) { + mUri = Uri.CREATOR.createFromParcel(in); + } else { + mUri = null; + } + mIsRead = in.readInt() > 0 ? true : false; + mHasContent = in.readInt() > 0 ? true : false; + mTranscription = (String) readCharSequence(in); + } + + private static CharSequence readCharSequence(Parcel in) { + return TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in); + } + + public static void writeCharSequence(Parcel dest, CharSequence val) { + TextUtils.writeToParcel(val, dest, 0); + } +} diff --git a/java/com/android/voicemail/impl/VoicemailClientImpl.java b/java/com/android/voicemail/impl/VoicemailClientImpl.java new file mode 100644 index 000000000..1ad12aeab --- /dev/null +++ b/java/com/android/voicemail/impl/VoicemailClientImpl.java @@ -0,0 +1,90 @@ +/** + * 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.voicemail.impl; + +import android.annotation.TargetApi; +import android.content.Context; +import android.os.Build.VERSION_CODES; +import android.provider.VoicemailContract.Voicemails; +import android.support.annotation.Nullable; +import android.support.v4.os.BuildCompat; +import android.telecom.PhoneAccountHandle; +import android.telephony.TelephonyManager; +import com.android.dialer.common.Assert; +import com.android.voicemail.VoicemailClient; +import com.android.voicemail.impl.settings.VisualVoicemailSettingsUtil; +import com.android.voicemail.impl.settings.VoicemailSettingsFragment; +import java.util.List; +import javax.inject.Inject; + +/** + * {@link VoicemailClient} to be used when the voicemail module is activated. May only be used above + * O. + */ +public class VoicemailClientImpl implements VoicemailClient { + + /** + * List of legacy OMTP voicemail packages that should be ignored. It could never be the active VVM + * package anymore. For example, voicemails in OC will no longer be handled by telephony, but + * legacy voicemails might still exist in the database due to upgrading from NYC. Dialer will + * fetch these voicemails again so it should be ignored. + */ + private static final String[] OMTP_VOICEMAIL_BLACKLIST = {"com.android.phone"}; + + @Inject + public VoicemailClientImpl() { + Assert.checkArgument(BuildCompat.isAtLeastO()); + } + + @Nullable + @Override + public String getSettingsFragment() { + return VoicemailSettingsFragment.class.getName(); + } + + @Override + public boolean isVoicemailArchiveEnabled(Context context, PhoneAccountHandle phoneAccountHandle) { + return VisualVoicemailSettingsUtil.isArchiveEnabled(context, phoneAccountHandle); + } + + @Override + public void setVoicemailArchiveEnabled( + Context context, PhoneAccountHandle phoneAccountHandle, boolean value) { + VisualVoicemailSettingsUtil.setArchiveEnabled(context, phoneAccountHandle, value); + } + + @TargetApi(VERSION_CODES.O) + @Override + public void appendOmtpVoicemailSelectionClause( + Context context, StringBuilder where, List selectionArgs) { + TelephonyManager telephonyManager = context.getSystemService(TelephonyManager.class); + String omtpSource = TelephonyMangerCompat.getVisualVoicemailPackageName(telephonyManager); + where.append( + "AND (" + + "(" + + Voicemails.IS_OMTP_VOICEMAIL + + " != 1)" + + "OR " + + "(" + + Voicemails.SOURCE_PACKAGE + + " = ? )" + + ")"); + selectionArgs.add(omtpSource); + + for (String blacklistedPackage : OMTP_VOICEMAIL_BLACKLIST) { + where.append("AND (" + Voicemails.SOURCE_PACKAGE + "!= ?)"); + selectionArgs.add(blacklistedPackage); + } + } +} diff --git a/java/com/android/voicemail/impl/VoicemailClientReceiver.java b/java/com/android/voicemail/impl/VoicemailClientReceiver.java new file mode 100644 index 000000000..49a55a41b --- /dev/null +++ b/java/com/android/voicemail/impl/VoicemailClientReceiver.java @@ -0,0 +1,51 @@ +/* + * 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.voicemail.impl; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.telecom.PhoneAccountHandle; +import com.android.dialer.common.Assert; +import com.android.dialer.common.LogUtil; +import com.android.voicemail.VoicemailClient; +import com.android.voicemail.impl.sync.UploadTask; +import com.android.voicemail.impl.sync.VvmAccountManager; + +/** Receiver for broadcasts in {@link VoicemailClient#ACTION_UPLOAD} */ +public class VoicemailClientReceiver extends BroadcastReceiver { + + @Override + public void onReceive(Context context, Intent intent) { + switch (intent.getAction()) { + case VoicemailClient.ACTION_UPLOAD: + doUpload(context); + break; + default: + Assert.fail("Unexpected action " + intent.getAction()); + break; + } + } + + /** Upload local database changes to the server. */ + private static void doUpload(Context context) { + LogUtil.i("VoicemailClientReceiver.onReceive", "ACTION_UPLOAD received"); + for (PhoneAccountHandle phoneAccountHandle : VvmAccountManager.getActiveAccounts(context)) { + UploadTask.start(context, phoneAccountHandle); + } + } +} diff --git a/java/com/android/voicemail/impl/VoicemailModule.java b/java/com/android/voicemail/impl/VoicemailModule.java new file mode 100644 index 000000000..c3e5714d5 --- /dev/null +++ b/java/com/android/voicemail/impl/VoicemailModule.java @@ -0,0 +1,41 @@ +/* + * 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.voicemail.impl; + +import android.support.v4.os.BuildCompat; +import com.android.voicemail.VoicemailClient; +import com.android.voicemail.stub.StubVoicemailClient; +import dagger.Module; +import dagger.Provides; +import javax.inject.Singleton; + +/** This module provides an instance of the voicemail client. */ +@Module +public final class VoicemailModule { + + @Provides + @Singleton + static VoicemailClient provideVoicemailClient() { + if (BuildCompat.isAtLeastO()) { + return new VoicemailClientImpl(); + } else { + return new StubVoicemailClient(); + } + } + + private VoicemailModule() {} +} diff --git a/java/com/android/voicemail/impl/VoicemailStatus.java b/java/com/android/voicemail/impl/VoicemailStatus.java new file mode 100644 index 000000000..ec1ab4e70 --- /dev/null +++ b/java/com/android/voicemail/impl/VoicemailStatus.java @@ -0,0 +1,160 @@ +/* + * 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.voicemail.impl; + +import android.content.ContentResolver; +import android.content.ContentValues; +import android.content.Context; +import android.net.Uri; +import android.provider.VoicemailContract; +import android.provider.VoicemailContract.Status; +import android.support.annotation.Nullable; +import android.telecom.PhoneAccountHandle; + +public class VoicemailStatus { + + private static final String TAG = "VvmStatus"; + + public static class Editor { + + private final Context mContext; + @Nullable private final PhoneAccountHandle mPhoneAccountHandle; + + private ContentValues mValues = new ContentValues(); + + private Editor(Context context, PhoneAccountHandle phoneAccountHandle) { + mContext = context; + mPhoneAccountHandle = phoneAccountHandle; + if (mPhoneAccountHandle == null) { + VvmLog.w( + TAG, + "VoicemailStatus.Editor created with null phone account, status will" + + " not be written"); + } + } + + @Nullable + public PhoneAccountHandle getPhoneAccountHandle() { + return mPhoneAccountHandle; + } + + public Editor setType(String type) { + mValues.put(Status.SOURCE_TYPE, type); + return this; + } + + public Editor setConfigurationState(int configurationState) { + mValues.put(Status.CONFIGURATION_STATE, configurationState); + return this; + } + + public Editor setDataChannelState(int dataChannelState) { + mValues.put(Status.DATA_CHANNEL_STATE, dataChannelState); + return this; + } + + public Editor setNotificationChannelState(int notificationChannelState) { + mValues.put(Status.NOTIFICATION_CHANNEL_STATE, notificationChannelState); + return this; + } + + public Editor setQuota(int occupied, int total) { + if (occupied == VoicemailContract.Status.QUOTA_UNAVAILABLE + && total == VoicemailContract.Status.QUOTA_UNAVAILABLE) { + return this; + } + + mValues.put(Status.QUOTA_OCCUPIED, occupied); + mValues.put(Status.QUOTA_TOTAL, total); + return this; + } + + /** + * Apply the changes to the {@link VoicemailStatus} {@link #Editor}. + * + * @return {@code true} if the changes were successfully applied, {@code false} otherwise. + */ + public boolean apply() { + if (mPhoneAccountHandle == null) { + return false; + } + mValues.put( + Status.PHONE_ACCOUNT_COMPONENT_NAME, + mPhoneAccountHandle.getComponentName().flattenToString()); + mValues.put(Status.PHONE_ACCOUNT_ID, mPhoneAccountHandle.getId()); + ContentResolver contentResolver = mContext.getContentResolver(); + Uri statusUri = VoicemailContract.Status.buildSourceUri(mContext.getPackageName()); + try { + contentResolver.insert(statusUri, mValues); + } catch (IllegalArgumentException iae) { + VvmLog.e(TAG, "apply :: failed to insert content resolver ", iae); + mValues.clear(); + return false; + } + mValues.clear(); + return true; + } + + public ContentValues getValues() { + return mValues; + } + } + + /** + * A voicemail status editor that the decision of whether to actually write to the database can be + * deferred. This object will be passed around as a usual {@link Editor}, but {@link #apply()} + * doesn't do anything. If later the creator of this object decides any status changes written to + * it should be committed, {@link #deferredApply()} should be called. + */ + public static class DeferredEditor extends Editor { + + private DeferredEditor(Context context, PhoneAccountHandle phoneAccountHandle) { + super(context, phoneAccountHandle); + } + + @Override + public boolean apply() { + // Do nothing + return true; + } + + public void deferredApply() { + super.apply(); + } + } + + public static Editor edit(Context context, PhoneAccountHandle phoneAccountHandle) { + return new Editor(context, phoneAccountHandle); + } + + /** + * Reset the status to the "disabled" state, which the UI should not show anything for this + * phoneAccountHandle. + */ + public static void disable(Context context, PhoneAccountHandle phoneAccountHandle) { + edit(context, phoneAccountHandle) + .setConfigurationState(Status.CONFIGURATION_STATE_NOT_CONFIGURED) + .setDataChannelState(Status.DATA_CHANNEL_STATE_NO_CONNECTION) + .setNotificationChannelState(Status.NOTIFICATION_CHANNEL_STATE_NO_CONNECTION) + .apply(); + } + + public static DeferredEditor deferredEdit( + Context context, PhoneAccountHandle phoneAccountHandle) { + return new DeferredEditor(context, phoneAccountHandle); + } +} diff --git a/java/com/android/voicemail/impl/VvmLog.java b/java/com/android/voicemail/impl/VvmLog.java new file mode 100644 index 000000000..595207f92 --- /dev/null +++ b/java/com/android/voicemail/impl/VvmLog.java @@ -0,0 +1,177 @@ +/* + * 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.voicemail.impl; + +import com.android.dialer.common.LogUtil; +import com.android.voicemail.impl.utils.IndentingPrintWriter; +import java.io.FileDescriptor; +import java.io.PrintWriter; +import java.util.ArrayDeque; +import java.util.Calendar; +import java.util.Deque; +import java.util.Iterator; + +/** Helper methods for adding to OMTP visual voicemail local logs. */ +public class VvmLog { + + private static final int MAX_OMTP_VVM_LOGS = 100; + + private static final LocalLog sLocalLog = new LocalLog(MAX_OMTP_VVM_LOGS); + + public static void log(String tag, String log) { + sLocalLog.log(tag + ": " + log); + } + + public static void dump(FileDescriptor fd, PrintWriter printwriter, String[] args) { + IndentingPrintWriter indentingPrintWriter = new IndentingPrintWriter(printwriter, " "); + indentingPrintWriter.increaseIndent(); + sLocalLog.dump(fd, indentingPrintWriter, args); + indentingPrintWriter.decreaseIndent(); + } + + public static void e(String tag, String log) { + log(tag, log); + LogUtil.e(tag, log); + } + + public static void e(String tag, String log, Throwable e) { + log(tag, log + " " + e); + LogUtil.e(tag, log, e); + } + + public static void w(String tag, String log) { + log(tag, log); + LogUtil.w(tag, log); + } + + public static void w(String tag, String log, Throwable e) { + log(tag, log + " " + e); + LogUtil.w(tag, log, e); + } + + public static void i(String tag, String log) { + log(tag, log); + LogUtil.i(tag, log); + } + + public static void i(String tag, String log, Throwable e) { + log(tag, log + " " + e); + LogUtil.i(tag, log, e); + } + + public static void d(String tag, String log) { + log(tag, log); + LogUtil.d(tag, log); + } + + public static void d(String tag, String log, Throwable e) { + log(tag, log + " " + e); + LogUtil.d(tag, log, e); + } + + public static void v(String tag, String log) { + log(tag, log); + LogUtil.v(tag, log); + } + + public static void v(String tag, String log, Throwable e) { + log(tag, log + " " + e); + LogUtil.v(tag, log, e); + } + + public static void wtf(String tag, String log) { + log(tag, log); + LogUtil.e(tag, log); + } + + public static void wtf(String tag, String log, Throwable e) { + log(tag, log + " " + e); + LogUtil.e(tag, log, e); + } + + /** + * Redact personally identifiable information for production users. If we are running in verbose + * mode, return the original string, otherwise return a SHA-1 hash of the input string. + */ + public static String pii(Object pii) { + if (pii == null) { + return String.valueOf(pii); + } + return "[PII]"; + } + + public static class LocalLog { + + private final Deque mLog; + private final int mMaxLines; + + public LocalLog(int maxLines) { + mMaxLines = Math.max(0, maxLines); + mLog = new ArrayDeque<>(mMaxLines); + } + + public void log(String msg) { + if (mMaxLines <= 0) { + return; + } + Calendar c = Calendar.getInstance(); + c.setTimeInMillis(System.currentTimeMillis()); + append(String.format("%tm-%td %tH:%tM:%tS.%tL - %s", c, c, c, c, c, c, msg)); + } + + private synchronized void append(String logLine) { + while (mLog.size() >= mMaxLines) { + mLog.remove(); + } + mLog.add(logLine); + } + + public synchronized void dump(FileDescriptor fd, PrintWriter pw, String[] args) { + Iterator itr = mLog.iterator(); + while (itr.hasNext()) { + pw.println(itr.next()); + } + } + + public synchronized void reverseDump(FileDescriptor fd, PrintWriter pw, String[] args) { + Iterator itr = mLog.descendingIterator(); + while (itr.hasNext()) { + pw.println(itr.next()); + } + } + + public static class ReadOnlyLocalLog { + + private final LocalLog mLog; + + ReadOnlyLocalLog(LocalLog log) { + mLog = log; + } + + public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { + mLog.dump(fd, pw, args); + } + + public void reverseDump(FileDescriptor fd, PrintWriter pw, String[] args) { + mLog.reverseDump(fd, pw, args); + } + } + + public ReadOnlyLocalLog readOnlyLocalLog() { + return new ReadOnlyLocalLog(this); + } + } +} diff --git a/java/com/android/voicemail/impl/VvmPackageInstallReceiver.java b/java/com/android/voicemail/impl/VvmPackageInstallReceiver.java new file mode 100644 index 000000000..c5650b3ee --- /dev/null +++ b/java/com/android/voicemail/impl/VvmPackageInstallReceiver.java @@ -0,0 +1,65 @@ +/* + * 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.voicemail.impl; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.telecom.PhoneAccountHandle; +import android.telecom.TelecomManager; +import com.android.voicemail.impl.settings.VisualVoicemailSettingsUtil; + +/** + * When a new package is installed, check if it matches any of the vvm carrier apps of the currently + * enabled dialer vvm sources. + */ +public class VvmPackageInstallReceiver extends BroadcastReceiver { + + private static final String TAG = "VvmPkgInstallReceiver"; + + @Override + public void onReceive(Context context, Intent intent) { + if (intent.getData() == null) { + return; + } + + String packageName = intent.getData().getSchemeSpecificPart(); + if (packageName == null) { + return; + } + + for (PhoneAccountHandle phoneAccount : + context.getSystemService(TelecomManager.class).getCallCapablePhoneAccounts()) { + if (VisualVoicemailSettingsUtil.isEnabledUserSet(context, phoneAccount)) { + // Skip the check if this voicemail source's setting is overridden by the user. + continue; + } + + OmtpVvmCarrierConfigHelper carrierConfigHelper = + new OmtpVvmCarrierConfigHelper(context, phoneAccount); + if (carrierConfigHelper.getCarrierVvmPackageNames() == null) { + continue; + } + if (carrierConfigHelper.getCarrierVvmPackageNames().contains(packageName)) { + // Force deactivate the client. The user can re-enable it in the settings. + // There is no need to update the settings for deactivation. At this point, if the + // default value is used it should be false because a carrier package is present. + VvmLog.i(TAG, "Carrier VVM package installed, disabling system VVM client"); + VisualVoicemailSettingsUtil.setEnabled(context, phoneAccount, false); + } + } + } +} diff --git a/java/com/android/voicemail/impl/VvmPhoneStateListener.java b/java/com/android/voicemail/impl/VvmPhoneStateListener.java new file mode 100644 index 000000000..48b72042c --- /dev/null +++ b/java/com/android/voicemail/impl/VvmPhoneStateListener.java @@ -0,0 +1,104 @@ +/* + * 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.voicemail.impl; + +import android.content.Context; +import android.telecom.PhoneAccountHandle; +import android.telephony.PhoneStateListener; +import android.telephony.ServiceState; +import com.android.voicemail.impl.sync.OmtpVvmSyncService; +import com.android.voicemail.impl.sync.SyncTask; +import com.android.voicemail.impl.sync.VoicemailStatusQueryHelper; +import com.android.voicemail.impl.sync.VvmAccountManager; + +/** + * Check if service is lost and indicate this in the voicemail status. TODO(b/35125657): Not used + * for now, restore it. + */ +public class VvmPhoneStateListener extends PhoneStateListener { + + private static final String TAG = "VvmPhoneStateListener"; + + private PhoneAccountHandle mPhoneAccount; + private Context mContext; + private int mPreviousState = -1; + + public VvmPhoneStateListener(Context context, PhoneAccountHandle accountHandle) { + // TODO: b/32637799 too much trouble to call super constructor through reflection, + // just use non-phoneAccountHandle version for now. + super(); + mContext = context; + mPhoneAccount = accountHandle; + } + + @Override + public void onServiceStateChanged(ServiceState serviceState) { + if (mPhoneAccount == null) { + VvmLog.e( + TAG, + "onServiceStateChanged on phoneAccount " + + mPhoneAccount + + " with invalid phoneAccountHandle, ignoring"); + return; + } + + int state = serviceState.getState(); + if (state == mPreviousState + || (state != ServiceState.STATE_IN_SERVICE + && mPreviousState != ServiceState.STATE_IN_SERVICE)) { + // Only interested in state changes or transitioning into or out of "in service". + // Otherwise just quit. + mPreviousState = state; + return; + } + + OmtpVvmCarrierConfigHelper helper = new OmtpVvmCarrierConfigHelper(mContext, mPhoneAccount); + + if (state == ServiceState.STATE_IN_SERVICE) { + VoicemailStatusQueryHelper voicemailStatusQueryHelper = + new VoicemailStatusQueryHelper(mContext); + if (voicemailStatusQueryHelper.isVoicemailSourceConfigured(mPhoneAccount)) { + if (!voicemailStatusQueryHelper.isNotificationsChannelActive(mPhoneAccount)) { + VvmLog.v(TAG, "Notifications channel is active for " + mPhoneAccount); + helper.handleEvent( + VoicemailStatus.edit(mContext, mPhoneAccount), OmtpEvents.NOTIFICATION_IN_SERVICE); + } + } + + if (VvmAccountManager.isAccountActivated(mContext, mPhoneAccount)) { + VvmLog.v(TAG, "Signal returned: requesting resync for " + mPhoneAccount); + // If the source is already registered, run a full sync in case something was missed + // while signal was down. + SyncTask.start(mContext, mPhoneAccount, OmtpVvmSyncService.SYNC_FULL_SYNC); + } else { + VvmLog.v(TAG, "Signal returned: reattempting activation for " + mPhoneAccount); + // Otherwise initiate an activation because this means that an OMTP source was + // recognized but either the activation text was not successfully sent or a response + // was not received. + helper.startActivation(); + } + } else { + VvmLog.v(TAG, "Notifications channel is inactive for " + mPhoneAccount); + + if (!VvmAccountManager.isAccountActivated(mContext, mPhoneAccount)) { + return; + } + helper.handleEvent( + VoicemailStatus.edit(mContext, mPhoneAccount), OmtpEvents.NOTIFICATION_SERVICE_LOST); + } + mPreviousState = state; + } +} diff --git a/java/com/android/voicemail/impl/fetch/FetchVoicemailReceiver.java b/java/com/android/voicemail/impl/fetch/FetchVoicemailReceiver.java new file mode 100644 index 000000000..07e800836 --- /dev/null +++ b/java/com/android/voicemail/impl/fetch/FetchVoicemailReceiver.java @@ -0,0 +1,218 @@ +/* + * 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.voicemail.impl.fetch; + +import android.annotation.TargetApi; +import android.content.BroadcastReceiver; +import android.content.ComponentName; +import android.content.ContentResolver; +import android.content.Context; +import android.content.Intent; +import android.database.Cursor; +import android.net.Network; +import android.net.Uri; +import android.os.Build.VERSION_CODES; +import android.provider.VoicemailContract; +import android.provider.VoicemailContract.Voicemails; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.support.v4.os.BuildCompat; +import android.telecom.PhoneAccountHandle; +import android.telecom.TelecomManager; +import android.telephony.TelephonyManager; +import android.text.TextUtils; +import com.android.voicemail.impl.VoicemailStatus; +import com.android.voicemail.impl.VvmLog; +import com.android.voicemail.impl.imap.ImapHelper; +import com.android.voicemail.impl.imap.ImapHelper.InitializingException; +import com.android.voicemail.impl.sync.VvmAccountManager; +import com.android.voicemail.impl.sync.VvmNetworkRequestCallback; +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; + +/** handles {@link VoicemailContract#ACTION_FETCH_VOICEMAIL} */ +@TargetApi(VERSION_CODES.O) +public class FetchVoicemailReceiver extends BroadcastReceiver { + + private static final String TAG = "FetchVoicemailReceiver"; + + static final String[] PROJECTION = + new String[] { + Voicemails.SOURCE_DATA, // 0 + Voicemails.PHONE_ACCOUNT_ID, // 1 + Voicemails.PHONE_ACCOUNT_COMPONENT_NAME, // 2 + }; + + public static final int SOURCE_DATA = 0; + public static final int PHONE_ACCOUNT_ID = 1; + public static final int PHONE_ACCOUNT_COMPONENT_NAME = 2; + + // Number of retries + private static final int NETWORK_RETRY_COUNT = 3; + + private ContentResolver mContentResolver; + private Uri mUri; + private VvmNetworkRequestCallback mNetworkCallback; + private Context mContext; + private String mUid; + private PhoneAccountHandle mPhoneAccount; + private int mRetryCount = NETWORK_RETRY_COUNT; + + @Override + public void onReceive(final Context context, Intent intent) { + if (VoicemailContract.ACTION_FETCH_VOICEMAIL.equals(intent.getAction())) { + VvmLog.i(TAG, "ACTION_FETCH_VOICEMAIL received"); + mContext = context; + mContentResolver = context.getContentResolver(); + mUri = intent.getData(); + + if (mUri == null) { + VvmLog.w(TAG, VoicemailContract.ACTION_FETCH_VOICEMAIL + " intent sent with no data"); + return; + } + + if (!context + .getPackageName() + .equals(mUri.getQueryParameter(VoicemailContract.PARAM_KEY_SOURCE_PACKAGE))) { + // Ignore if the fetch request is for a voicemail not from this package. + VvmLog.e(TAG, "ACTION_FETCH_VOICEMAIL from foreign pacakge " + context.getPackageName()); + return; + } + + Cursor cursor = mContentResolver.query(mUri, PROJECTION, null, null, null); + if (cursor == null) { + VvmLog.i(TAG, "ACTION_FETCH_VOICEMAIL query returned null"); + return; + } + try { + if (cursor.moveToFirst()) { + mUid = cursor.getString(SOURCE_DATA); + String accountId = cursor.getString(PHONE_ACCOUNT_ID); + if (TextUtils.isEmpty(accountId)) { + TelephonyManager telephonyManager = + (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); + accountId = telephonyManager.getSimSerialNumber(); + + if (TextUtils.isEmpty(accountId)) { + VvmLog.e(TAG, "Account null and no default sim found."); + return; + } + } + + mPhoneAccount = + new PhoneAccountHandle( + ComponentName.unflattenFromString(cursor.getString(PHONE_ACCOUNT_COMPONENT_NAME)), + cursor.getString(PHONE_ACCOUNT_ID)); + if (!VvmAccountManager.isAccountActivated(context, mPhoneAccount)) { + mPhoneAccount = getAccountFromMarshmallowAccount(context, mPhoneAccount); + if (mPhoneAccount == null) { + VvmLog.w(TAG, "Account not registered - cannot retrieve message."); + return; + } + VvmLog.i(TAG, "Fetching voicemail with Marshmallow PhoneAccountHandle"); + } + VvmLog.i(TAG, "Requesting network to fetch voicemail"); + mNetworkCallback = new fetchVoicemailNetworkRequestCallback(context, mPhoneAccount); + mNetworkCallback.requestNetwork(); + } + } finally { + cursor.close(); + } + } + } + + /** + * In ag/930496 the format of PhoneAccountHandle has changed between Marshmallow and Nougat. This + * method attempts to search the account from the old database in registered sources using the old + * format. There's a chance of M phone account collisions on multi-SIM devices, but visual + * voicemail is not supported on M multi-SIM. + */ + @Nullable + private static PhoneAccountHandle getAccountFromMarshmallowAccount( + Context context, PhoneAccountHandle oldAccount) { + if (!BuildCompat.isAtLeastN()) { + return null; + } + for (PhoneAccountHandle handle : + context.getSystemService(TelecomManager.class).getCallCapablePhoneAccounts()) { + if (getIccSerialNumberFromFullIccSerialNumber(handle.getId()).equals(oldAccount.getId())) { + return handle; + } + } + return null; + } + + /** + * getIccSerialNumber() is used for ID before N, and getFullIccSerialNumber() after. + * getIccSerialNumber() stops at the first hex char. + */ + @NonNull + private static String getIccSerialNumberFromFullIccSerialNumber(@NonNull String id) { + for (int i = 0; i < id.length(); i++) { + if (!Character.isDigit(id.charAt(i))) { + return id.substring(0, i); + } + } + return id; + } + + private class fetchVoicemailNetworkRequestCallback extends VvmNetworkRequestCallback { + + public fetchVoicemailNetworkRequestCallback(Context context, PhoneAccountHandle phoneAccount) { + super(context, phoneAccount, VoicemailStatus.edit(context, phoneAccount)); + } + + @Override + public void onAvailable(final Network network) { + super.onAvailable(network); + fetchVoicemail(network, getVoicemailStatusEditor()); + } + } + + private void fetchVoicemail(final Network network, final VoicemailStatus.Editor status) { + Executor executor = Executors.newCachedThreadPool(); + executor.execute( + new Runnable() { + @Override + public void run() { + try { + while (mRetryCount > 0) { + VvmLog.i(TAG, "fetching voicemail, retry count=" + mRetryCount); + try (ImapHelper imapHelper = + new ImapHelper(mContext, mPhoneAccount, network, status)) { + boolean success = + imapHelper.fetchVoicemailPayload( + new VoicemailFetchedCallback(mContext, mUri, mPhoneAccount), mUid); + if (!success && mRetryCount > 0) { + VvmLog.i(TAG, "fetch voicemail failed, retrying"); + mRetryCount--; + } else { + return; + } + } catch (InitializingException e) { + VvmLog.w(TAG, "Can't retrieve Imap credentials ", e); + return; + } + } + } finally { + if (mNetworkCallback != null) { + mNetworkCallback.releaseNetwork(); + } + } + } + }); + } +} diff --git a/java/com/android/voicemail/impl/fetch/VoicemailFetchedCallback.java b/java/com/android/voicemail/impl/fetch/VoicemailFetchedCallback.java new file mode 100644 index 000000000..f386fce0e --- /dev/null +++ b/java/com/android/voicemail/impl/fetch/VoicemailFetchedCallback.java @@ -0,0 +1,102 @@ +/* + * 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.voicemail.impl.fetch; + +import android.content.ContentResolver; +import android.content.ContentValues; +import android.content.Context; +import android.net.Uri; +import android.provider.VoicemailContract.Voicemails; +import android.support.annotation.Nullable; +import android.telecom.PhoneAccountHandle; +import android.telecom.TelecomManager; +import com.android.voicemail.impl.R; +import com.android.voicemail.impl.VvmLog; +import com.android.voicemail.impl.imap.VoicemailPayload; +import java.io.IOException; +import java.io.OutputStream; +import org.apache.commons.io.IOUtils; + +/** + * Callback for when a voicemail payload is fetched. It copies the returned stream to the data file + * corresponding to the voicemail. + */ +public class VoicemailFetchedCallback { + private static final String TAG = "VoicemailFetchedCallback"; + + private final Context mContext; + private final ContentResolver mContentResolver; + private final Uri mUri; + private final PhoneAccountHandle mPhoneAccountHandle; + + public VoicemailFetchedCallback(Context context, Uri uri, PhoneAccountHandle phoneAccountHandle) { + mContext = context; + mContentResolver = context.getContentResolver(); + mUri = uri; + mPhoneAccountHandle = phoneAccountHandle; + } + + /** + * Saves the voicemail payload data into the voicemail provider then sets the "has_content" bit of + * the voicemail to "1". + * + * @param voicemailPayload The object containing the content data for the voicemail + */ + public void setVoicemailContent(@Nullable VoicemailPayload voicemailPayload) { + if (voicemailPayload == null) { + VvmLog.i(TAG, "Payload not found, message has unsupported format"); + ContentValues values = new ContentValues(); + values.put( + Voicemails.TRANSCRIPTION, + mContext.getString( + R.string.vvm_unsupported_message_format, + mContext + .getSystemService(TelecomManager.class) + .getVoiceMailNumber(mPhoneAccountHandle))); + updateVoicemail(values); + return; + } + + VvmLog.d(TAG, String.format("Writing new voicemail content: %s", mUri)); + OutputStream outputStream = null; + + try { + outputStream = mContentResolver.openOutputStream(mUri); + byte[] inputBytes = voicemailPayload.getBytes(); + if (inputBytes != null) { + outputStream.write(inputBytes); + } + } catch (IOException e) { + VvmLog.w(TAG, String.format("File not found for %s", mUri)); + return; + } finally { + IOUtils.closeQuietly(outputStream); + } + + // Update mime_type & has_content after we are done with file update. + ContentValues values = new ContentValues(); + values.put(Voicemails.MIME_TYPE, voicemailPayload.getMimeType()); + values.put(Voicemails.HAS_CONTENT, true); + updateVoicemail(values); + } + + private void updateVoicemail(ContentValues values) { + int updatedCount = mContentResolver.update(mUri, values, null, null); + if (updatedCount != 1) { + VvmLog.e(TAG, "Updating voicemail should have updated 1 row, was: " + updatedCount); + } + } +} diff --git a/java/com/android/voicemail/impl/imap/ImapHelper.java b/java/com/android/voicemail/impl/imap/ImapHelper.java new file mode 100644 index 000000000..6aa415811 --- /dev/null +++ b/java/com/android/voicemail/impl/imap/ImapHelper.java @@ -0,0 +1,693 @@ +/* + * 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.voicemail.impl.imap; + +import android.content.Context; +import android.net.ConnectivityManager; +import android.net.Network; +import android.net.NetworkInfo; +import android.provider.VoicemailContract; +import android.telecom.PhoneAccountHandle; +import android.util.Base64; +import com.android.voicemail.impl.OmtpConstants; +import com.android.voicemail.impl.OmtpConstants.ChangePinResult; +import com.android.voicemail.impl.OmtpEvents; +import com.android.voicemail.impl.OmtpVvmCarrierConfigHelper; +import com.android.voicemail.impl.VisualVoicemailPreferences; +import com.android.voicemail.impl.Voicemail; +import com.android.voicemail.impl.VoicemailStatus; +import com.android.voicemail.impl.VoicemailStatus.Editor; +import com.android.voicemail.impl.VvmLog; +import com.android.voicemail.impl.fetch.VoicemailFetchedCallback; +import com.android.voicemail.impl.mail.Address; +import com.android.voicemail.impl.mail.Body; +import com.android.voicemail.impl.mail.BodyPart; +import com.android.voicemail.impl.mail.FetchProfile; +import com.android.voicemail.impl.mail.Flag; +import com.android.voicemail.impl.mail.Message; +import com.android.voicemail.impl.mail.MessagingException; +import com.android.voicemail.impl.mail.Multipart; +import com.android.voicemail.impl.mail.TempDirectory; +import com.android.voicemail.impl.mail.internet.MimeMessage; +import com.android.voicemail.impl.mail.store.ImapConnection; +import com.android.voicemail.impl.mail.store.ImapFolder; +import com.android.voicemail.impl.mail.store.ImapStore; +import com.android.voicemail.impl.mail.store.imap.ImapConstants; +import com.android.voicemail.impl.mail.store.imap.ImapResponse; +import com.android.voicemail.impl.mail.utils.LogUtils; +import com.android.voicemail.impl.sync.OmtpVvmSyncService.TranscriptionFetchedCallback; +import java.io.BufferedOutputStream; +import java.io.ByteArrayOutputStream; +import java.io.Closeable; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Locale; +import org.apache.commons.io.IOUtils; + +/** A helper interface to abstract commands sent across IMAP interface for a given account. */ +public class ImapHelper implements Closeable { + + private static final String TAG = "ImapHelper"; + + private ImapFolder mFolder; + private ImapStore mImapStore; + + private final Context mContext; + private final PhoneAccountHandle mPhoneAccount; + private final Network mNetwork; + private final Editor mStatus; + + VisualVoicemailPreferences mPrefs; + private static final String PREF_KEY_QUOTA_OCCUPIED = "quota_occupied_"; + private static final String PREF_KEY_QUOTA_TOTAL = "quota_total_"; + + private int mQuotaOccupied; + private int mQuotaTotal; + + private final OmtpVvmCarrierConfigHelper mConfig; + + /** InitializingException */ + public static class InitializingException extends Exception { + + public InitializingException(String message) { + super(message); + } + } + + public ImapHelper( + Context context, + PhoneAccountHandle phoneAccount, + Network network, + Editor status) + throws InitializingException { + this( + context, + new OmtpVvmCarrierConfigHelper(context, phoneAccount), + phoneAccount, + network, + status); + } + + public ImapHelper( + Context context, + OmtpVvmCarrierConfigHelper config, + PhoneAccountHandle phoneAccount, + Network network, + Editor status) + throws InitializingException { + mContext = context; + mPhoneAccount = phoneAccount; + mNetwork = network; + mStatus = status; + mConfig = config; + mPrefs = new VisualVoicemailPreferences(context, phoneAccount); + + try { + TempDirectory.setTempDirectory(context); + + String username = mPrefs.getString(OmtpConstants.IMAP_USER_NAME, null); + String password = mPrefs.getString(OmtpConstants.IMAP_PASSWORD, null); + String serverName = mPrefs.getString(OmtpConstants.SERVER_ADDRESS, null); + int port = Integer.parseInt(mPrefs.getString(OmtpConstants.IMAP_PORT, null)); + int auth = ImapStore.FLAG_NONE; + + int sslPort = mConfig.getSslPort(); + if (sslPort != 0) { + port = sslPort; + auth = ImapStore.FLAG_SSL; + } + + mImapStore = + new ImapStore(context, this, username, password, port, serverName, auth, network); + } catch (NumberFormatException e) { + handleEvent(OmtpEvents.DATA_INVALID_PORT); + LogUtils.w(TAG, "Could not parse port number"); + throw new InitializingException("cannot initialize ImapHelper:" + e.toString()); + } + + mQuotaOccupied = + mPrefs.getInt(PREF_KEY_QUOTA_OCCUPIED, VoicemailContract.Status.QUOTA_UNAVAILABLE); + mQuotaTotal = mPrefs.getInt(PREF_KEY_QUOTA_TOTAL, VoicemailContract.Status.QUOTA_UNAVAILABLE); + } + + @Override + public void close() { + mImapStore.closeConnection(); + } + + public boolean isRoaming() { + ConnectivityManager connectivityManager = + (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); + NetworkInfo info = connectivityManager.getNetworkInfo(mNetwork); + if (info == null) { + return false; + } + return info.isRoaming(); + } + + public OmtpVvmCarrierConfigHelper getConfig() { + return mConfig; + } + + public ImapConnection connect() { + return mImapStore.getConnection(); + } + + /** The caller thread will block until the method returns. */ + public boolean markMessagesAsRead(List voicemails) { + return setFlags(voicemails, Flag.SEEN); + } + + /** The caller thread will block until the method returns. */ + public boolean markMessagesAsDeleted(List voicemails) { + return setFlags(voicemails, Flag.DELETED); + } + + public void handleEvent(OmtpEvents event) { + mConfig.handleEvent(mStatus, event); + } + + /** + * Set flags on the server for a given set of voicemails. + * + * @param voicemails The voicemails to set flags for. + * @param flags The flags to set on the voicemails. + * @return {@code true} if the operation completes successfully, {@code false} otherwise. + */ + private boolean setFlags(List voicemails, String... flags) { + if (voicemails.size() == 0) { + return false; + } + try { + mFolder = openImapFolder(ImapFolder.MODE_READ_WRITE); + if (mFolder != null) { + mFolder.setFlags(convertToImapMessages(voicemails), flags, true); + return true; + } + return false; + } catch (MessagingException e) { + LogUtils.e(TAG, e, "Messaging exception"); + return false; + } finally { + closeImapFolder(); + } + } + + /** + * Fetch a list of voicemails from the server. + * + * @return A list of voicemail objects containing data about voicemails stored on the server. + */ + public List fetchAllVoicemails() { + List result = new ArrayList(); + Message[] messages; + try { + mFolder = openImapFolder(ImapFolder.MODE_READ_WRITE); + if (mFolder == null) { + // This means we were unable to successfully open the folder. + return null; + } + + // This method retrieves lightweight messages containing only the uid of the message. + messages = mFolder.getMessages(null); + + for (Message message : messages) { + // Get the voicemail details (message structure). + MessageStructureWrapper messageStructureWrapper = fetchMessageStructure(message); + if (messageStructureWrapper != null) { + result.add(getVoicemailFromMessageStructure(messageStructureWrapper)); + } + } + return result; + } catch (MessagingException e) { + LogUtils.e(TAG, e, "Messaging Exception"); + return null; + } finally { + closeImapFolder(); + } + } + + /** + * Extract voicemail details from the message structure. Also fetch transcription if a + * transcription exists. + */ + private Voicemail getVoicemailFromMessageStructure( + MessageStructureWrapper messageStructureWrapper) throws MessagingException { + Message messageDetails = messageStructureWrapper.messageStructure; + + TranscriptionFetchedListener listener = new TranscriptionFetchedListener(); + if (messageStructureWrapper.transcriptionBodyPart != null) { + FetchProfile fetchProfile = new FetchProfile(); + fetchProfile.add(messageStructureWrapper.transcriptionBodyPart); + + mFolder.fetch(new Message[] {messageDetails}, fetchProfile, listener); + } + + // Found an audio attachment, this is a valid voicemail. + long time = messageDetails.getSentDate().getTime(); + String number = getNumber(messageDetails.getFrom()); + boolean isRead = Arrays.asList(messageDetails.getFlags()).contains(Flag.SEEN); + return Voicemail.createForInsertion(time, number) + .setPhoneAccount(mPhoneAccount) + .setSourcePackage(mContext.getPackageName()) + .setSourceData(messageDetails.getUid()) + .setIsRead(isRead) + .setTranscription(listener.getVoicemailTranscription()) + .build(); + } + + /** + * The "from" field of a visual voicemail IMAP message is the number of the caller who left the + * message. Extract this number from the list of "from" addresses. + * + * @param fromAddresses A list of addresses that comprise the "from" line. + * @return The number of the voicemail sender. + */ + private String getNumber(Address[] fromAddresses) { + if (fromAddresses != null && fromAddresses.length > 0) { + if (fromAddresses.length != 1) { + LogUtils.w(TAG, "More than one from addresses found. Using the first one."); + } + String sender = fromAddresses[0].getAddress(); + int atPos = sender.indexOf('@'); + if (atPos != -1) { + // Strip domain part of the address. + sender = sender.substring(0, atPos); + } + return sender; + } + return null; + } + + /** + * Fetches the structure of the given message and returns a wrapper containing the message + * structure and the transcription structure (if applicable). + * + * @throws MessagingException if fetching the structure of the message fails + */ + private MessageStructureWrapper fetchMessageStructure(Message message) throws MessagingException { + LogUtils.d(TAG, "Fetching message structure for " + message.getUid()); + + MessageStructureFetchedListener listener = new MessageStructureFetchedListener(); + + FetchProfile fetchProfile = new FetchProfile(); + fetchProfile.addAll( + Arrays.asList( + FetchProfile.Item.FLAGS, FetchProfile.Item.ENVELOPE, FetchProfile.Item.STRUCTURE)); + + // The IMAP folder fetch method will call "messageRetrieved" on the listener when the + // message is successfully retrieved. + mFolder.fetch(new Message[] {message}, fetchProfile, listener); + return listener.getMessageStructure(); + } + + public boolean fetchVoicemailPayload(VoicemailFetchedCallback callback, final String uid) { + try { + mFolder = openImapFolder(ImapFolder.MODE_READ_WRITE); + if (mFolder == null) { + // This means we were unable to successfully open the folder. + return false; + } + Message message = mFolder.getMessage(uid); + if (message == null) { + return false; + } + VoicemailPayload voicemailPayload = fetchVoicemailPayload(message); + callback.setVoicemailContent(voicemailPayload); + return true; + } catch (MessagingException e) { + } finally { + closeImapFolder(); + } + return false; + } + + /** + * Fetches the body of the given message and returns the parsed voicemail payload. + * + * @throws MessagingException if fetching the body of the message fails + */ + private VoicemailPayload fetchVoicemailPayload(Message message) throws MessagingException { + LogUtils.d(TAG, "Fetching message body for " + message.getUid()); + + MessageBodyFetchedListener listener = new MessageBodyFetchedListener(); + + FetchProfile fetchProfile = new FetchProfile(); + fetchProfile.add(FetchProfile.Item.BODY); + + mFolder.fetch(new Message[] {message}, fetchProfile, listener); + return listener.getVoicemailPayload(); + } + + public boolean fetchTranscription(TranscriptionFetchedCallback callback, String uid) { + try { + mFolder = openImapFolder(ImapFolder.MODE_READ_WRITE); + if (mFolder == null) { + // This means we were unable to successfully open the folder. + return false; + } + + Message message = mFolder.getMessage(uid); + if (message == null) { + return false; + } + + MessageStructureWrapper messageStructureWrapper = fetchMessageStructure(message); + if (messageStructureWrapper != null) { + TranscriptionFetchedListener listener = new TranscriptionFetchedListener(); + if (messageStructureWrapper.transcriptionBodyPart != null) { + FetchProfile fetchProfile = new FetchProfile(); + fetchProfile.add(messageStructureWrapper.transcriptionBodyPart); + + // This method is called synchronously so the transcription will be populated + // in the listener once the next method is called. + mFolder.fetch(new Message[] {message}, fetchProfile, listener); + callback.setVoicemailTranscription(listener.getVoicemailTranscription()); + } + } + return true; + } catch (MessagingException e) { + LogUtils.e(TAG, e, "Messaging Exception"); + return false; + } finally { + closeImapFolder(); + } + } + + @ChangePinResult + public int changePin(String oldPin, String newPin) throws MessagingException { + ImapConnection connection = mImapStore.getConnection(); + try { + String command = + getConfig().getProtocol().getCommand(OmtpConstants.IMAP_CHANGE_TUI_PWD_FORMAT); + connection.sendCommand(String.format(Locale.US, command, newPin, oldPin), true); + return getChangePinResultFromImapResponse(connection.readResponse()); + } catch (IOException ioe) { + VvmLog.e(TAG, "changePin: ", ioe); + return OmtpConstants.CHANGE_PIN_SYSTEM_ERROR; + } finally { + connection.destroyResponses(); + } + } + + public void changeVoicemailTuiLanguage(String languageCode) throws MessagingException { + ImapConnection connection = mImapStore.getConnection(); + try { + String command = + getConfig().getProtocol().getCommand(OmtpConstants.IMAP_CHANGE_VM_LANG_FORMAT); + connection.sendCommand(String.format(Locale.US, command, languageCode), true); + } catch (IOException ioe) { + LogUtils.e(TAG, ioe.toString()); + } finally { + connection.destroyResponses(); + } + } + + public void closeNewUserTutorial() throws MessagingException { + ImapConnection connection = mImapStore.getConnection(); + try { + String command = getConfig().getProtocol().getCommand(OmtpConstants.IMAP_CLOSE_NUT); + connection.executeSimpleCommand(command, false); + } catch (IOException ioe) { + throw new MessagingException(MessagingException.SERVER_ERROR, ioe.toString()); + } finally { + connection.destroyResponses(); + } + } + + @ChangePinResult + private static int getChangePinResultFromImapResponse(ImapResponse response) + throws MessagingException { + if (!response.isTagged()) { + throw new MessagingException(MessagingException.SERVER_ERROR, "tagged response expected"); + } + if (!response.isOk()) { + String message = response.getStringOrEmpty(1).getString(); + LogUtils.d(TAG, "change PIN failed: " + message); + if (OmtpConstants.RESPONSE_CHANGE_PIN_TOO_SHORT.equals(message)) { + return OmtpConstants.CHANGE_PIN_TOO_SHORT; + } + if (OmtpConstants.RESPONSE_CHANGE_PIN_TOO_LONG.equals(message)) { + return OmtpConstants.CHANGE_PIN_TOO_LONG; + } + if (OmtpConstants.RESPONSE_CHANGE_PIN_TOO_WEAK.equals(message)) { + return OmtpConstants.CHANGE_PIN_TOO_WEAK; + } + if (OmtpConstants.RESPONSE_CHANGE_PIN_MISMATCH.equals(message)) { + return OmtpConstants.CHANGE_PIN_MISMATCH; + } + if (OmtpConstants.RESPONSE_CHANGE_PIN_INVALID_CHARACTER.equals(message)) { + return OmtpConstants.CHANGE_PIN_INVALID_CHARACTER; + } + return OmtpConstants.CHANGE_PIN_SYSTEM_ERROR; + } + LogUtils.d(TAG, "change PIN succeeded"); + return OmtpConstants.CHANGE_PIN_SUCCESS; + } + + public void updateQuota() { + try { + mFolder = openImapFolder(ImapFolder.MODE_READ_WRITE); + if (mFolder == null) { + // This means we were unable to successfully open the folder. + return; + } + updateQuota(mFolder); + } catch (MessagingException e) { + LogUtils.e(TAG, e, "Messaging Exception"); + } finally { + closeImapFolder(); + } + } + + public int getOccuupiedQuota() { + return mQuotaOccupied; + } + + public int getTotalQuota() { + return mQuotaTotal; + } + + private void updateQuota(ImapFolder folder) throws MessagingException { + setQuota(folder.getQuota()); + } + + private void setQuota(ImapFolder.Quota quota) { + if (quota == null) { + return; + } + if (quota.occupied == mQuotaOccupied && quota.total == mQuotaTotal) { + VvmLog.v(TAG, "Quota hasn't changed"); + return; + } + mQuotaOccupied = quota.occupied; + mQuotaTotal = quota.total; + VoicemailStatus.edit(mContext, mPhoneAccount).setQuota(mQuotaOccupied, mQuotaTotal).apply(); + mPrefs + .edit() + .putInt(PREF_KEY_QUOTA_OCCUPIED, mQuotaOccupied) + .putInt(PREF_KEY_QUOTA_TOTAL, mQuotaTotal) + .apply(); + VvmLog.v(TAG, "Quota changed to " + mQuotaOccupied + "/" + mQuotaTotal); + } + + /** + * A wrapper to hold a message with its header details and the structure for transcriptions (so + * they can be fetched in the future). + */ + public static class MessageStructureWrapper { + + public Message messageStructure; + public BodyPart transcriptionBodyPart; + + public MessageStructureWrapper() {} + } + + /** Listener for the message structure being fetched. */ + private final class MessageStructureFetchedListener + implements ImapFolder.MessageRetrievalListener { + + private MessageStructureWrapper mMessageStructure; + + public MessageStructureFetchedListener() {} + + public MessageStructureWrapper getMessageStructure() { + return mMessageStructure; + } + + @Override + public void messageRetrieved(Message message) { + LogUtils.d(TAG, "Fetched message structure for " + message.getUid()); + LogUtils.d(TAG, "Message retrieved: " + message); + try { + mMessageStructure = getMessageOrNull(message); + if (mMessageStructure == null) { + LogUtils.d(TAG, "This voicemail does not have an attachment..."); + return; + } + } catch (MessagingException e) { + LogUtils.e(TAG, e, "Messaging Exception"); + closeImapFolder(); + } + } + + /** + * Check if this IMAP message is a valid voicemail and whether it contains a transcription. + * + * @param message The IMAP message. + * @return The MessageStructureWrapper object corresponding to an IMAP message and + * transcription. + */ + private MessageStructureWrapper getMessageOrNull(Message message) throws MessagingException { + if (!message.getMimeType().startsWith("multipart/")) { + LogUtils.w(TAG, "Ignored non multi-part message"); + return null; + } + + MessageStructureWrapper messageStructureWrapper = new MessageStructureWrapper(); + + Multipart multipart = (Multipart) message.getBody(); + for (int i = 0; i < multipart.getCount(); ++i) { + BodyPart bodyPart = multipart.getBodyPart(i); + String bodyPartMimeType = bodyPart.getMimeType().toLowerCase(); + LogUtils.d(TAG, "bodyPart mime type: " + bodyPartMimeType); + + if (bodyPartMimeType.startsWith("audio/")) { + messageStructureWrapper.messageStructure = message; + } else if (bodyPartMimeType.startsWith("text/")) { + messageStructureWrapper.transcriptionBodyPart = bodyPart; + } else { + VvmLog.v(TAG, "Unknown bodyPart MIME: " + bodyPartMimeType); + } + } + + if (messageStructureWrapper.messageStructure != null) { + return messageStructureWrapper; + } + + // No attachment found, this is not a voicemail. + return null; + } + } + + /** Listener for the message body being fetched. */ + private final class MessageBodyFetchedListener implements ImapFolder.MessageRetrievalListener { + + private VoicemailPayload mVoicemailPayload; + + /** Returns the fetch voicemail payload. */ + public VoicemailPayload getVoicemailPayload() { + return mVoicemailPayload; + } + + @Override + public void messageRetrieved(Message message) { + LogUtils.d(TAG, "Fetched message body for " + message.getUid()); + LogUtils.d(TAG, "Message retrieved: " + message); + try { + mVoicemailPayload = getVoicemailPayloadFromMessage(message); + } catch (MessagingException e) { + LogUtils.e(TAG, "Messaging Exception:", e); + } catch (IOException e) { + LogUtils.e(TAG, "IO Exception:", e); + } + } + + private VoicemailPayload getVoicemailPayloadFromMessage(Message message) + throws MessagingException, IOException { + Multipart multipart = (Multipart) message.getBody(); + List mimeTypes = new ArrayList<>(); + for (int i = 0; i < multipart.getCount(); ++i) { + BodyPart bodyPart = multipart.getBodyPart(i); + String bodyPartMimeType = bodyPart.getMimeType().toLowerCase(); + mimeTypes.add(bodyPartMimeType); + if (bodyPartMimeType.startsWith("audio/")) { + byte[] bytes = getDataFromBody(bodyPart.getBody()); + LogUtils.d(TAG, String.format("Fetched %s bytes of data", bytes.length)); + return new VoicemailPayload(bodyPartMimeType, bytes); + } + } + LogUtils.e(TAG, "No audio attachment found on this voicemail, mimeTypes:" + mimeTypes); + return null; + } + } + + /** Listener for the transcription being fetched. */ + private final class TranscriptionFetchedListener implements ImapFolder.MessageRetrievalListener { + + private String mVoicemailTranscription; + + /** Returns the fetched voicemail transcription. */ + public String getVoicemailTranscription() { + return mVoicemailTranscription; + } + + @Override + public void messageRetrieved(Message message) { + LogUtils.d(TAG, "Fetched transcription for " + message.getUid()); + try { + mVoicemailTranscription = new String(getDataFromBody(message.getBody())); + } catch (MessagingException e) { + LogUtils.e(TAG, "Messaging Exception:", e); + } catch (IOException e) { + LogUtils.e(TAG, "IO Exception:", e); + } + } + } + + private ImapFolder openImapFolder(String modeReadWrite) { + try { + if (mImapStore == null) { + return null; + } + ImapFolder folder = new ImapFolder(mImapStore, ImapConstants.INBOX); + folder.open(modeReadWrite); + return folder; + } catch (MessagingException e) { + LogUtils.e(TAG, e, "Messaging Exception"); + } + return null; + } + + private Message[] convertToImapMessages(List voicemails) { + Message[] messages = new Message[voicemails.size()]; + for (int i = 0; i < voicemails.size(); ++i) { + messages[i] = new MimeMessage(); + messages[i].setUid(voicemails.get(i).getSourceData()); + } + return messages; + } + + private void closeImapFolder() { + if (mFolder != null) { + mFolder.close(true); + } + } + + private byte[] getDataFromBody(Body body) throws IOException, MessagingException { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + BufferedOutputStream bufferedOut = new BufferedOutputStream(out); + try { + body.writeTo(bufferedOut); + return Base64.decode(out.toByteArray(), Base64.DEFAULT); + } finally { + IOUtils.closeQuietly(bufferedOut); + IOUtils.closeQuietly(out); + } + } +} diff --git a/java/com/android/voicemail/impl/imap/VoicemailPayload.java b/java/com/android/voicemail/impl/imap/VoicemailPayload.java new file mode 100644 index 000000000..69befb42f --- /dev/null +++ b/java/com/android/voicemail/impl/imap/VoicemailPayload.java @@ -0,0 +1,36 @@ +/* + * 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.voicemail.impl.imap; + +/** The payload for a voicemail, usually audio data. */ +public class VoicemailPayload { + private final String mMimeType; + private final byte[] mBytes; + + public VoicemailPayload(String mimeType, byte[] bytes) { + mMimeType = mimeType; + mBytes = bytes; + } + + public byte[] getBytes() { + return mBytes; + } + + public String getMimeType() { + return mMimeType; + } +} diff --git a/java/com/android/voicemail/impl/mail/Address.java b/java/com/android/voicemail/impl/mail/Address.java new file mode 100644 index 000000000..3a7a86607 --- /dev/null +++ b/java/com/android/voicemail/impl/mail/Address.java @@ -0,0 +1,520 @@ +/* + * 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.voicemail.impl.mail; + +import android.os.Parcel; +import android.os.Parcelable; +import android.support.annotation.VisibleForTesting; +import android.text.Html; +import android.text.TextUtils; +import android.text.util.Rfc822Token; +import android.text.util.Rfc822Tokenizer; +import com.android.voicemail.impl.mail.utils.LogUtils; +import java.util.ArrayList; +import java.util.regex.Pattern; +import org.apache.james.mime4j.codec.EncoderUtil; +import org.apache.james.mime4j.decoder.DecoderUtil; + +/** + * This class represent email address. + * + *

RFC822 email address may have following format. "name"

(comment) "name"
+ * name
address Name and comment part should be MIME/base64 encoded in header if + * necessary. + */ +public class Address implements Parcelable { + public static final String ADDRESS_DELIMETER = ","; + /** Address part, in the form local_part@domain_part. No surrounding angle brackets. */ + private String mAddress; + + /** + * Name part. No surrounding double quote, and no MIME/base64 encoding. This must be null if + * Address has no name part. + */ + private String mPersonal; + + /** + * When personal is set, it will return the first token of the personal string. Otherwise, it will + * return the e-mail address up to the '@' sign. + */ + private String mSimplifiedName; + + // Regex that matches address surrounded by '<>' optionally. '^]+)>?$' + private static final Pattern REMOVE_OPTIONAL_BRACKET = Pattern.compile("^]+)>?$"); + // Regex that matches personal name surrounded by '""' optionally. '^"?([^"]+)"?$' + private static final Pattern REMOVE_OPTIONAL_DQUOTE = Pattern.compile("^\"?([^\"]*)\"?$"); + // Regex that matches escaped character '\\([\\"])' + private static final Pattern UNQUOTE = Pattern.compile("\\\\([\\\\\"])"); + + // TODO: LOCAL_PART and DOMAIN_PART_PART are too permissive and can be improved. + // TODO: Fix this to better constrain comments. + /** Regex for the local part of an email address. */ + private static final String LOCAL_PART = "[^@]+"; + /** Regex for each part of the domain part, i.e. the thing between the dots. */ + private static final String DOMAIN_PART_PART = "[[\\w][\\d]\\-\\(\\)\\[\\]]+"; + /** Regex for the domain part, which is two or more {@link #DOMAIN_PART_PART} separated by . */ + private static final String DOMAIN_PART = "(" + DOMAIN_PART_PART + "\\.)+" + DOMAIN_PART_PART; + + /** Pattern to check if an email address is valid. */ + private static final Pattern EMAIL_ADDRESS = + Pattern.compile("\\A" + LOCAL_PART + "@" + DOMAIN_PART + "\\z"); + + private static final Address[] EMPTY_ADDRESS_ARRAY = new Address[0]; + + // delimiters are chars that do not appear in an email address, used by fromHeader + private static final char LIST_DELIMITER_EMAIL = '\1'; + private static final char LIST_DELIMITER_PERSONAL = '\2'; + + private static final String LOG_TAG = "Email Address"; + + @VisibleForTesting + public Address(String address) { + setAddress(address); + } + + public Address(String address, String personal) { + setPersonal(personal); + setAddress(address); + } + + /** + * Returns a simplified string for this e-mail address. When a name is known, it will return the + * first token of that name. Otherwise, it will return the e-mail address up to the '@' sign. + */ + public String getSimplifiedName() { + if (mSimplifiedName == null) { + if (TextUtils.isEmpty(mPersonal) && !TextUtils.isEmpty(mAddress)) { + int atSign = mAddress.indexOf('@'); + mSimplifiedName = (atSign != -1) ? mAddress.substring(0, atSign) : ""; + } else if (!TextUtils.isEmpty(mPersonal)) { + + // TODO: use Contacts' NameSplitter for more reliable first-name extraction + + int end = mPersonal.indexOf(' '); + while (end > 0 && mPersonal.charAt(end - 1) == ',') { + end--; + } + mSimplifiedName = (end < 1) ? mPersonal : mPersonal.substring(0, end); + + } else { + LogUtils.w(LOG_TAG, "Unable to get a simplified name"); + mSimplifiedName = ""; + } + } + return mSimplifiedName; + } + + public static synchronized Address getEmailAddress(String rawAddress) { + if (TextUtils.isEmpty(rawAddress)) { + return null; + } + String name, address; + final Rfc822Token[] tokens = Rfc822Tokenizer.tokenize(rawAddress); + if (tokens.length > 0) { + final String tokenizedName = tokens[0].getName(); + name = tokenizedName != null ? Html.fromHtml(tokenizedName.trim()).toString() : ""; + address = Html.fromHtml(tokens[0].getAddress()).toString(); + } else { + name = ""; + address = rawAddress == null ? "" : Html.fromHtml(rawAddress).toString(); + } + return new Address(address, name); + } + + public String getAddress() { + return mAddress; + } + + public void setAddress(String address) { + mAddress = REMOVE_OPTIONAL_BRACKET.matcher(address).replaceAll("$1"); + } + + /** + * Get name part as UTF-16 string. No surrounding double quote, and no MIME/base64 encoding. + * + * @return Name part of email address. Returns null if it is omitted. + */ + public String getPersonal() { + return mPersonal; + } + + /** + * Set personal part from UTF-16 string. Optional surrounding double quote will be removed. It + * will be also unquoted and MIME/base64 decoded. + * + * @param personal name part of email address as UTF-16 string. Null is acceptable. + */ + public void setPersonal(String personal) { + mPersonal = decodeAddressPersonal(personal); + } + + /** + * Decodes name from UTF-16 string. Optional surrounding double quote will be removed. It will be + * also unquoted and MIME/base64 decoded. + * + * @param personal name part of email address as UTF-16 string. Null is acceptable. + */ + public static String decodeAddressPersonal(String personal) { + if (personal != null) { + personal = REMOVE_OPTIONAL_DQUOTE.matcher(personal).replaceAll("$1"); + personal = UNQUOTE.matcher(personal).replaceAll("$1"); + personal = DecoderUtil.decodeEncodedWords(personal); + if (personal.length() == 0) { + personal = null; + } + } + return personal; + } + + /** + * This method is used to check that all the addresses that the user entered in a list (e.g. To:) + * are valid, so that none is dropped. + */ + @VisibleForTesting + public static boolean isAllValid(String addressList) { + // This code mimics the parse() method below. + // I don't know how to better avoid the code-duplication. + if (addressList != null && addressList.length() > 0) { + Rfc822Token[] tokens = Rfc822Tokenizer.tokenize(addressList); + for (int i = 0, length = tokens.length; i < length; ++i) { + Rfc822Token token = tokens[i]; + String address = token.getAddress(); + if (!TextUtils.isEmpty(address) && !isValidAddress(address)) { + return false; + } + } + } + return true; + } + + /** + * Parse a comma-delimited list of addresses in RFC822 format and return an array of Address + * objects. + * + * @param addressList Address list in comma-delimited string. + * @return An array of 0 or more Addresses. + */ + public static Address[] parse(String addressList) { + if (addressList == null || addressList.length() == 0) { + return EMPTY_ADDRESS_ARRAY; + } + Rfc822Token[] tokens = Rfc822Tokenizer.tokenize(addressList); + ArrayList
addresses = new ArrayList
(); + for (int i = 0, length = tokens.length; i < length; ++i) { + Rfc822Token token = tokens[i]; + String address = token.getAddress(); + if (!TextUtils.isEmpty(address)) { + if (isValidAddress(address)) { + String name = token.getName(); + if (TextUtils.isEmpty(name)) { + name = null; + } + addresses.add(new Address(address, name)); + } + } + } + return addresses.toArray(new Address[addresses.size()]); + } + + /** Checks whether a string email address is valid. E.g. name@domain.com is valid. */ + @VisibleForTesting + static boolean isValidAddress(final String address) { + return EMAIL_ADDRESS.matcher(address).find(); + } + + @Override + public boolean equals(Object o) { + if (o instanceof Address) { + // It seems that the spec says that the "user" part is case-sensitive, + // while the domain part in case-insesitive. + // So foo@yahoo.com and Foo@yahoo.com are different. + // This may seem non-intuitive from the user POV, so we + // may re-consider it if it creates UI trouble. + // A problem case is "replyAll" sending to both + // a@b.c and to A@b.c, which turn out to be the same on the server. + // Leave unchanged for now (i.e. case-sensitive). + return getAddress().equals(((Address) o).getAddress()); + } + return super.equals(o); + } + + @Override + public int hashCode() { + return getAddress().hashCode(); + } + + /** + * Get human readable address string. Do not use this for email header. + * + * @return Human readable address string. Not quoted and not encoded. + */ + @Override + public String toString() { + if (mPersonal != null && !mPersonal.equals(mAddress)) { + if (mPersonal.matches(".*[\\(\\)<>@,;:\\\\\".\\[\\]].*")) { + return ensureQuotedString(mPersonal) + " <" + mAddress + ">"; + } else { + return mPersonal + " <" + mAddress + ">"; + } + } else { + return mAddress; + } + } + + /** + * Ensures that the given string starts and ends with the double quote character. The string is + * not modified in any way except to add the double quote character to start and end if it's not + * already there. + * + *

sample -> "sample" "sample" -> "sample" ""sample"" -> "sample" "sample"" -> "sample" + * sa"mp"le -> "sa"mp"le" "sa"mp"le" -> "sa"mp"le" (empty string) -> "" " -> "" + */ + private static String ensureQuotedString(String s) { + if (s == null) { + return null; + } + if (!s.matches("^\".*\"$")) { + return "\"" + s + "\""; + } else { + return s; + } + } + + /** + * Get human readable comma-delimited address string. + * + * @param addresses Address array + * @return Human readable comma-delimited address string. + */ + @VisibleForTesting + public static String toString(Address[] addresses) { + return toString(addresses, ADDRESS_DELIMETER); + } + + /** + * Get human readable address strings joined with the specified separator. + * + * @param addresses Address array + * @param separator Separator + * @return Human readable comma-delimited address string. + */ + public static String toString(Address[] addresses, String separator) { + if (addresses == null || addresses.length == 0) { + return null; + } + if (addresses.length == 1) { + return addresses[0].toString(); + } + StringBuilder sb = new StringBuilder(addresses[0].toString()); + for (int i = 1; i < addresses.length; i++) { + sb.append(separator); + // TODO: investigate why this .trim() is needed. + sb.append(addresses[i].toString().trim()); + } + return sb.toString(); + } + + /** + * Get RFC822/MIME compatible address string. + * + * @return RFC822/MIME compatible address string. It may be surrounded by double quote or quoted + * and MIME/base64 encoded if necessary. + */ + public String toHeader() { + if (mPersonal != null) { + return EncoderUtil.encodeAddressDisplayName(mPersonal) + " <" + mAddress + ">"; + } else { + return mAddress; + } + } + + /** + * Get RFC822/MIME compatible comma-delimited address string. + * + * @param addresses Address array + * @return RFC822/MIME compatible comma-delimited address string. it may be surrounded by double + * quoted or quoted and MIME/base64 encoded if necessary. + */ + public static String toHeader(Address[] addresses) { + if (addresses == null || addresses.length == 0) { + return null; + } + if (addresses.length == 1) { + return addresses[0].toHeader(); + } + StringBuilder sb = new StringBuilder(addresses[0].toHeader()); + for (int i = 1; i < addresses.length; i++) { + // We need space character to be able to fold line. + sb.append(", "); + sb.append(addresses[i].toHeader()); + } + return sb.toString(); + } + + /** + * Get Human friendly address string. + * + * @return the personal part of this Address, or the address part if the personal part is not + * available + */ + @VisibleForTesting + public String toFriendly() { + if (mPersonal != null && mPersonal.length() > 0) { + return mPersonal; + } else { + return mAddress; + } + } + + /** + * Creates a comma-delimited list of addresses in the "friendly" format (see toFriendly() for + * details on the per-address conversion). + * + * @param addresses Array of Address[] values + * @return A comma-delimited string listing all of the addresses supplied. Null if source was null + * or empty. + */ + @VisibleForTesting + public static String toFriendly(Address[] addresses) { + if (addresses == null || addresses.length == 0) { + return null; + } + if (addresses.length == 1) { + return addresses[0].toFriendly(); + } + StringBuilder sb = new StringBuilder(addresses[0].toFriendly()); + for (int i = 1; i < addresses.length; i++) { + sb.append(", "); + sb.append(addresses[i].toFriendly()); + } + return sb.toString(); + } + + /** Returns exactly the same result as Address.toString(Address.fromHeader(addressList)). */ + @VisibleForTesting + public static String fromHeaderToString(String addressList) { + return toString(fromHeader(addressList)); + } + + /** Returns exactly the same result as Address.toHeader(Address.parse(addressList)). */ + @VisibleForTesting + public static String parseToHeader(String addressList) { + return Address.toHeader(Address.parse(addressList)); + } + + /** + * Returns null if the addressList has 0 addresses, otherwise returns the first address. The same + * as Address.fromHeader(addressList)[0] for non-empty list. This is an utility method that offers + * some performance optimization opportunities. + */ + @VisibleForTesting + public static Address firstAddress(String addressList) { + Address[] array = fromHeader(addressList); + return array.length > 0 ? array[0] : null; + } + + /** + * This method exists to convert an address list formatted in a deprecated legacy format to the + * standard RFC822 header format. {@link #fromHeader(String)} is capable of reading the legacy + * format and the RFC822 format. {@link #toHeader()} always produces the RFC822 format. + * + *

This implementation is brute-force, and could be replaced with a more efficient version if + * desired. + */ + public static String reformatToHeader(String addressList) { + return toHeader(fromHeader(addressList)); + } + + /** + * @param addressList a CSV of RFC822 addresses or the deprecated legacy string format + * @return array of addresses parsed from addressList + */ + @VisibleForTesting + public static Address[] fromHeader(String addressList) { + if (addressList == null || addressList.length() == 0) { + return EMPTY_ADDRESS_ARRAY; + } + // IF we're CSV, just parse + if ((addressList.indexOf(LIST_DELIMITER_PERSONAL) == -1) + && (addressList.indexOf(LIST_DELIMITER_EMAIL) == -1)) { + return Address.parse(addressList); + } + // Otherwise, do backward-compatible unpack + ArrayList

addresses = new ArrayList
(); + int length = addressList.length(); + int pairStartIndex = 0; + int pairEndIndex; + + /* addressEndIndex is only re-scanned (indexOf()) when a LIST_DELIMITER_PERSONAL + is used, not for every email address; i.e. not for every iteration of the while(). + This reduces the theoretical complexity from quadratic to linear, + and provides some speed-up in practice by removing redundant scans of the string. + */ + int addressEndIndex = addressList.indexOf(LIST_DELIMITER_PERSONAL); + + while (pairStartIndex < length) { + pairEndIndex = addressList.indexOf(LIST_DELIMITER_EMAIL, pairStartIndex); + if (pairEndIndex == -1) { + pairEndIndex = length; + } + Address address; + if (addressEndIndex == -1 || pairEndIndex <= addressEndIndex) { + // in this case the DELIMITER_PERSONAL is in a future pair, + // so don't use personal, and don't update addressEndIndex + address = new Address(addressList.substring(pairStartIndex, pairEndIndex), null); + } else { + address = + new Address( + addressList.substring(pairStartIndex, addressEndIndex), + addressList.substring(addressEndIndex + 1, pairEndIndex)); + // only update addressEndIndex when we use the LIST_DELIMITER_PERSONAL + addressEndIndex = addressList.indexOf(LIST_DELIMITER_PERSONAL, pairEndIndex + 1); + } + addresses.add(address); + pairStartIndex = pairEndIndex + 1; + } + return addresses.toArray(new Address[addresses.size()]); + } + + public static final Creator
CREATOR = + new Creator
() { + @Override + public Address createFromParcel(Parcel parcel) { + return new Address(parcel); + } + + @Override + public Address[] newArray(int size) { + return new Address[size]; + } + }; + + public Address(Parcel in) { + setPersonal(in.readString()); + setAddress(in.readString()); + } + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(Parcel out, int flags) { + out.writeString(mPersonal); + out.writeString(mAddress); + } +} diff --git a/java/com/android/voicemail/impl/mail/AuthenticationFailedException.java b/java/com/android/voicemail/impl/mail/AuthenticationFailedException.java new file mode 100644 index 000000000..c9fa08750 --- /dev/null +++ b/java/com/android/voicemail/impl/mail/AuthenticationFailedException.java @@ -0,0 +1,33 @@ +/* + * 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.voicemail.impl.mail; + +public class AuthenticationFailedException extends MessagingException { + public static final long serialVersionUID = -1; + + public AuthenticationFailedException(String message) { + super(MessagingException.AUTHENTICATION_FAILED, message); + } + + public AuthenticationFailedException(int exceptionType, String message) { + super(exceptionType, message); + } + + public AuthenticationFailedException(String message, Throwable throwable) { + super(MessagingException.AUTHENTICATION_FAILED, message, throwable); + } +} diff --git a/java/com/android/voicemail/impl/mail/Base64Body.java b/java/com/android/voicemail/impl/mail/Base64Body.java new file mode 100644 index 000000000..def94dbb5 --- /dev/null +++ b/java/com/android/voicemail/impl/mail/Base64Body.java @@ -0,0 +1,61 @@ +/* + * 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.voicemail.impl.mail; + +import android.util.Base64; +import android.util.Base64OutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import org.apache.commons.io.IOUtils; + +public class Base64Body implements Body { + private final InputStream mSource; + // Because we consume the input stream, we can only write out once + private boolean mAlreadyWritten; + + public Base64Body(InputStream source) { + mSource = source; + } + + @Override + public InputStream getInputStream() throws MessagingException { + return mSource; + } + + /** + * This method consumes the input stream, so can only be called once + * + * @param out Stream to write to + * @throws IllegalStateException If called more than once + * @throws IOException + * @throws MessagingException + */ + @Override + public void writeTo(OutputStream out) + throws IllegalStateException, IOException, MessagingException { + if (mAlreadyWritten) { + throw new IllegalStateException("Base64Body can only be written once"); + } + mAlreadyWritten = true; + try { + final Base64OutputStream b64out = new Base64OutputStream(out, Base64.DEFAULT); + IOUtils.copyLarge(mSource, b64out); + } finally { + mSource.close(); + } + } +} diff --git a/java/com/android/voicemail/impl/mail/Body.java b/java/com/android/voicemail/impl/mail/Body.java new file mode 100644 index 000000000..3ad81bcc8 --- /dev/null +++ b/java/com/android/voicemail/impl/mail/Body.java @@ -0,0 +1,26 @@ +/* + * 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.voicemail.impl.mail; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +public interface Body { + public InputStream getInputStream() throws MessagingException; + + public void writeTo(OutputStream out) throws IOException, MessagingException; +} diff --git a/java/com/android/voicemail/impl/mail/BodyPart.java b/java/com/android/voicemail/impl/mail/BodyPart.java new file mode 100644 index 000000000..3d15d4bad --- /dev/null +++ b/java/com/android/voicemail/impl/mail/BodyPart.java @@ -0,0 +1,24 @@ +/* + * 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.voicemail.impl.mail; + +public abstract class BodyPart implements Part { + protected Multipart mParent; + + public Multipart getParent() { + return mParent; + } +} diff --git a/java/com/android/voicemail/impl/mail/CertificateValidationException.java b/java/com/android/voicemail/impl/mail/CertificateValidationException.java new file mode 100644 index 000000000..6f3bb2ff4 --- /dev/null +++ b/java/com/android/voicemail/impl/mail/CertificateValidationException.java @@ -0,0 +1,29 @@ +/* + * 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.voicemail.impl.mail; + +public class CertificateValidationException extends MessagingException { + public static final long serialVersionUID = -1; + + public CertificateValidationException(String message) { + super(CERTIFICATE_VALIDATION_ERROR, message); + } + + public CertificateValidationException(String message, Throwable throwable) { + super(CERTIFICATE_VALIDATION_ERROR, message, throwable); + } +} diff --git a/java/com/android/voicemail/impl/mail/FetchProfile.java b/java/com/android/voicemail/impl/mail/FetchProfile.java new file mode 100644 index 000000000..28a7080e6 --- /dev/null +++ b/java/com/android/voicemail/impl/mail/FetchProfile.java @@ -0,0 +1,79 @@ +/* + * 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.voicemail.impl.mail; + +import java.util.ArrayList; + +/** + * + * + *
+ * A FetchProfile is a list of items that should be downloaded in bulk for a set of messages.
+ * FetchProfile can contain the following objects:
+ *      FetchProfile.Item:      Described below.
+ *      Message:                Indicates that the body of the entire message should be fetched.
+ *                              Synonymous with FetchProfile.Item.BODY.
+ *      Part:                   Indicates that the given Part should be fetched. The provider
+ *                              is expected have previously created the given BodyPart and stored
+ *                              any information it needs to download the content.
+ * 
+ */ +public class FetchProfile extends ArrayList { + /** + * Default items available for pre-fetching. It should be expected that any item fetched by using + * these items could potentially include all of the previous items. + */ + public enum Item implements Fetchable { + /** Download the flags of the message. */ + FLAGS, + + /** + * Download the envelope of the message. This should include at minimum the size and the + * following headers: date, subject, from, content-type, to, cc + */ + ENVELOPE, + + /** + * Download the structure of the message. This maps directly to IMAP's BODYSTRUCTURE and may map + * to other providers. The provider should, if possible, fill in a properly formatted MIME + * structure in the message without actually downloading any message data. If the provider is + * not capable of this operation it should specifically set the body of the message to null so + * that upper levels can detect that a full body download is needed. + */ + STRUCTURE, + + /** + * A sane portion of the entire message, cut off at a provider determined limit. This should + * generally be around 50kB. + */ + BODY_SANE, + + /** The entire message. */ + BODY, + } + + /** + * @return the first {@link Part} in this collection, or null if it doesn't contain {@link Part}. + */ + public Part getFirstPart() { + for (Fetchable o : this) { + if (o instanceof Part) { + return (Part) o; + } + } + return null; + } +} diff --git a/java/com/android/voicemail/impl/mail/Fetchable.java b/java/com/android/voicemail/impl/mail/Fetchable.java new file mode 100644 index 000000000..237ef6950 --- /dev/null +++ b/java/com/android/voicemail/impl/mail/Fetchable.java @@ -0,0 +1,22 @@ +/* + * 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.voicemail.impl.mail; + +/** + * Interface for classes that can be added to {@link FetchProfile}. i.e. {@link Part} and its + * subclasses, and {@link FetchProfile.Item}. + */ +public interface Fetchable {} diff --git a/java/com/android/voicemail/impl/mail/FixedLengthInputStream.java b/java/com/android/voicemail/impl/mail/FixedLengthInputStream.java new file mode 100644 index 000000000..bd3c16401 --- /dev/null +++ b/java/com/android/voicemail/impl/mail/FixedLengthInputStream.java @@ -0,0 +1,79 @@ +/* + * 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.voicemail.impl.mail; + +import java.io.IOException; +import java.io.InputStream; + +/** + * A filtering InputStream that stops allowing reads after the given length has been read. This is + * used to allow a client to read directly from an underlying protocol stream without reading past + * where the protocol handler intended the client to read. + */ +public class FixedLengthInputStream extends InputStream { + private final InputStream mIn; + private final int mLength; + private int mCount; + + public FixedLengthInputStream(InputStream in, int length) { + this.mIn = in; + this.mLength = length; + } + + @Override + public int available() throws IOException { + return mLength - mCount; + } + + @Override + public int read() throws IOException { + if (mCount < mLength) { + mCount++; + return mIn.read(); + } else { + return -1; + } + } + + @Override + public int read(byte[] b, int offset, int length) throws IOException { + if (mCount < mLength) { + int d = mIn.read(b, offset, Math.min(mLength - mCount, length)); + if (d == -1) { + return -1; + } else { + mCount += d; + return d; + } + } else { + return -1; + } + } + + @Override + public int read(byte[] b) throws IOException { + return read(b, 0, b.length); + } + + public int getLength() { + return mLength; + } + + @Override + public String toString() { + return String.format("FixedLengthInputStream(in=%s, length=%d)", mIn.toString(), mLength); + } +} diff --git a/java/com/android/voicemail/impl/mail/Flag.java b/java/com/android/voicemail/impl/mail/Flag.java new file mode 100644 index 000000000..72b5c1fa5 --- /dev/null +++ b/java/com/android/voicemail/impl/mail/Flag.java @@ -0,0 +1,27 @@ +/* + * 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.voicemail.impl.mail; + +/** Flags that can be applied to Messages. */ +public class Flag { + // If adding new flags: ALL FLAGS MUST BE UPPER CASE. + public static final String DELETED = "deleted"; + public static final String SEEN = "seen"; + public static final String ANSWERED = "answered"; + public static final String FLAGGED = "flagged"; + public static final String DRAFT = "draft"; + public static final String RECENT = "recent"; +} diff --git a/java/com/android/voicemail/impl/mail/MailTransport.java b/java/com/android/voicemail/impl/mail/MailTransport.java new file mode 100644 index 000000000..3df36d544 --- /dev/null +++ b/java/com/android/voicemail/impl/mail/MailTransport.java @@ -0,0 +1,343 @@ +/* + * 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.voicemail.impl.mail; + +import android.content.Context; +import android.net.Network; +import android.support.annotation.VisibleForTesting; +import com.android.voicemail.impl.OmtpEvents; +import com.android.voicemail.impl.imap.ImapHelper; +import com.android.voicemail.impl.mail.store.ImapStore; +import com.android.voicemail.impl.mail.utils.LogUtils; +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.Socket; +import java.util.ArrayList; +import java.util.List; +import javax.net.ssl.HostnameVerifier; +import javax.net.ssl.HttpsURLConnection; +import javax.net.ssl.SSLException; +import javax.net.ssl.SSLPeerUnverifiedException; +import javax.net.ssl.SSLSession; +import javax.net.ssl.SSLSocket; + +/** Make connection and perform operations on mail server by reading and writing lines. */ +public class MailTransport { + private static final String TAG = "MailTransport"; + + // TODO protected eventually + /*protected*/ public static final int SOCKET_CONNECT_TIMEOUT = 10000; + /*protected*/ public static final int SOCKET_READ_TIMEOUT = 60000; + + private static final HostnameVerifier HOSTNAME_VERIFIER = + HttpsURLConnection.getDefaultHostnameVerifier(); + + private final Context mContext; + private final ImapHelper mImapHelper; + private final Network mNetwork; + private final String mHost; + private final int mPort; + private Socket mSocket; + private BufferedInputStream mIn; + private BufferedOutputStream mOut; + private final int mFlags; + private SocketCreator mSocketCreator; + private InetSocketAddress mAddress; + + public MailTransport( + Context context, + ImapHelper imapHelper, + Network network, + String address, + int port, + int flags) { + mContext = context; + mImapHelper = imapHelper; + mNetwork = network; + mHost = address; + mPort = port; + mFlags = flags; + } + + /** + * Returns a new transport, using the current transport as a model. The new transport is + * configured identically, but not opened or connected in any way. + */ + @Override + public MailTransport clone() { + return new MailTransport(mContext, mImapHelper, mNetwork, mHost, mPort, mFlags); + } + + public boolean canTrySslSecurity() { + return (mFlags & ImapStore.FLAG_SSL) != 0; + } + + public boolean canTrustAllCertificates() { + return (mFlags & ImapStore.FLAG_TRUST_ALL) != 0; + } + + /** + * Attempts to open a connection using the Uri supplied for connection parameters. Will attempt an + * SSL connection if indicated. + */ + public void open() throws MessagingException { + LogUtils.d(TAG, "*** IMAP open " + mHost + ":" + String.valueOf(mPort)); + + List socketAddresses = new ArrayList(); + + if (mNetwork == null) { + socketAddresses.add(new InetSocketAddress(mHost, mPort)); + } else { + try { + InetAddress[] inetAddresses = mNetwork.getAllByName(mHost); + if (inetAddresses.length == 0) { + throw new MessagingException( + MessagingException.IOERROR, + "Host name " + mHost + "cannot be resolved on designated network"); + } + for (int i = 0; i < inetAddresses.length; i++) { + socketAddresses.add(new InetSocketAddress(inetAddresses[i], mPort)); + } + } catch (IOException ioe) { + LogUtils.d(TAG, ioe.toString()); + mImapHelper.handleEvent(OmtpEvents.DATA_CANNOT_RESOLVE_HOST_ON_NETWORK); + throw new MessagingException(MessagingException.IOERROR, ioe.toString()); + } + } + + boolean success = false; + while (socketAddresses.size() > 0) { + mSocket = createSocket(); + try { + mAddress = socketAddresses.remove(0); + mSocket.connect(mAddress, SOCKET_CONNECT_TIMEOUT); + + if (canTrySslSecurity()) { + /* + SSLSocket cannot be created with a connection timeout, so instead of doing a + direct SSL connection, we connect with a normal connection and upgrade it into + SSL + */ + reopenTls(); + } else { + mIn = new BufferedInputStream(mSocket.getInputStream(), 1024); + mOut = new BufferedOutputStream(mSocket.getOutputStream(), 512); + mSocket.setSoTimeout(SOCKET_READ_TIMEOUT); + } + success = true; + return; + } catch (IOException ioe) { + LogUtils.d(TAG, ioe.toString()); + if (socketAddresses.size() == 0) { + // Only throw an error when there are no more sockets to try. + mImapHelper.handleEvent(OmtpEvents.DATA_ALL_SOCKET_CONNECTION_FAILED); + throw new MessagingException(MessagingException.IOERROR, ioe.toString()); + } + } finally { + if (!success) { + try { + mSocket.close(); + mSocket = null; + } catch (IOException ioe) { + throw new MessagingException(MessagingException.IOERROR, ioe.toString()); + } + } + } + } + } + + // For testing. We need something that can replace the behavior of "new Socket()" + @VisibleForTesting + interface SocketCreator { + + Socket createSocket() throws MessagingException; + } + + @VisibleForTesting + void setSocketCreator(SocketCreator creator) { + mSocketCreator = creator; + } + + protected Socket createSocket() throws MessagingException { + if (mSocketCreator != null) { + return mSocketCreator.createSocket(); + } + + if (mNetwork == null) { + LogUtils.v(TAG, "createSocket: network not specified"); + return new Socket(); + } + + try { + LogUtils.v(TAG, "createSocket: network specified"); + return mNetwork.getSocketFactory().createSocket(); + } catch (IOException ioe) { + LogUtils.d(TAG, ioe.toString()); + throw new MessagingException(MessagingException.IOERROR, ioe.toString()); + } + } + + /** Attempts to reopen a normal connection into a TLS connection. */ + public void reopenTls() throws MessagingException { + try { + LogUtils.d(TAG, "open: converting to TLS socket"); + mSocket = + HttpsURLConnection.getDefaultSSLSocketFactory() + .createSocket(mSocket, mAddress.getHostName(), mAddress.getPort(), true); + // After the socket connects to an SSL server, confirm that the hostname is as + // expected + if (!canTrustAllCertificates()) { + verifyHostname(mSocket, mHost); + } + mSocket.setSoTimeout(SOCKET_READ_TIMEOUT); + mIn = new BufferedInputStream(mSocket.getInputStream(), 1024); + mOut = new BufferedOutputStream(mSocket.getOutputStream(), 512); + + } catch (SSLException e) { + LogUtils.d(TAG, e.toString()); + throw new CertificateValidationException(e.getMessage(), e); + } catch (IOException ioe) { + LogUtils.d(TAG, ioe.toString()); + throw new MessagingException(MessagingException.IOERROR, ioe.toString()); + } + } + + /** + * Lightweight version of SSLCertificateSocketFactory.verifyHostname, which provides this service + * but is not in the public API. + * + *

Verify the hostname of the certificate used by the other end of a connected socket. It is + * harmless to call this method redundantly if the hostname has already been verified. + * + *

Wildcard certificates are allowed to verify any matching hostname, so "foo.bar.example.com" + * is verified if the peer has a certificate for "*.example.com". + * + * @param socket An SSL socket which has been connected to a server + * @param hostname The expected hostname of the remote server + * @throws IOException if something goes wrong handshaking with the server + * @throws SSLPeerUnverifiedException if the server cannot prove its identity + */ + private void verifyHostname(Socket socket, String hostname) throws IOException { + // The code at the start of OpenSSLSocketImpl.startHandshake() + // ensures that the call is idempotent, so we can safely call it. + SSLSocket ssl = (SSLSocket) socket; + ssl.startHandshake(); + + SSLSession session = ssl.getSession(); + if (session == null) { + mImapHelper.handleEvent(OmtpEvents.DATA_CANNOT_ESTABLISH_SSL_SESSION); + throw new SSLException("Cannot verify SSL socket without session"); + } + // TODO: Instead of reporting the name of the server we think we're connecting to, + // we should be reporting the bad name in the certificate. Unfortunately this is buried + // in the verifier code and is not available in the verifier API, and extracting the + // CN & alts is beyond the scope of this patch. + if (!HOSTNAME_VERIFIER.verify(hostname, session)) { + mImapHelper.handleEvent(OmtpEvents.DATA_SSL_INVALID_HOST_NAME); + throw new SSLPeerUnverifiedException( + "Certificate hostname not useable for server: " + session.getPeerPrincipal()); + } + } + + public boolean isOpen() { + return (mIn != null + && mOut != null + && mSocket != null + && mSocket.isConnected() + && !mSocket.isClosed()); + } + + /** Close the connection. MUST NOT return any exceptions - must be "best effort" and safe. */ + public void close() { + try { + mIn.close(); + } catch (Exception e) { + // May fail if the connection is already closed. + } + try { + mOut.close(); + } catch (Exception e) { + // May fail if the connection is already closed. + } + try { + mSocket.close(); + } catch (Exception e) { + // May fail if the connection is already closed. + } + mIn = null; + mOut = null; + mSocket = null; + } + + public String getHost() { + return mHost; + } + + public InputStream getInputStream() { + return mIn; + } + + public OutputStream getOutputStream() { + return mOut; + } + + /** Writes a single line to the server using \r\n termination. */ + public void writeLine(String s, String sensitiveReplacement) throws IOException { + if (sensitiveReplacement != null) { + LogUtils.d(TAG, ">>> " + sensitiveReplacement); + } else { + LogUtils.d(TAG, ">>> " + s); + } + + OutputStream out = getOutputStream(); + out.write(s.getBytes()); + out.write('\r'); + out.write('\n'); + out.flush(); + } + + /** + * Reads a single line from the server, using either \r\n or \n as the delimiter. The delimiter + * char(s) are not included in the result. + */ + public String readLine(boolean loggable) throws IOException { + StringBuffer sb = new StringBuffer(); + InputStream in = getInputStream(); + int d; + while ((d = in.read()) != -1) { + if (((char) d) == '\r') { + continue; + } else if (((char) d) == '\n') { + break; + } else { + sb.append((char) d); + } + } + if (d == -1) { + LogUtils.d(TAG, "End of stream reached while trying to read line."); + } + String ret = sb.toString(); + if (loggable) { + LogUtils.d(TAG, "<<< " + ret); + } + return ret; + } +} diff --git a/java/com/android/voicemail/impl/mail/MeetingInfo.java b/java/com/android/voicemail/impl/mail/MeetingInfo.java new file mode 100644 index 000000000..9fe953d5d --- /dev/null +++ b/java/com/android/voicemail/impl/mail/MeetingInfo.java @@ -0,0 +1,29 @@ +/* + * 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.voicemail.impl.mail; + +public class MeetingInfo { + // Predefined tags; others can be added + public static final String MEETING_DTSTAMP = "DTSTAMP"; + public static final String MEETING_UID = "UID"; + public static final String MEETING_ORGANIZER_EMAIL = "ORGMAIL"; + public static final String MEETING_DTSTART = "DTSTART"; + public static final String MEETING_DTEND = "DTEND"; + public static final String MEETING_TITLE = "TITLE"; + public static final String MEETING_LOCATION = "LOC"; + public static final String MEETING_RESPONSE_REQUESTED = "RESPONSE"; + public static final String MEETING_ALL_DAY = "ALLDAY"; +} diff --git a/java/com/android/voicemail/impl/mail/Message.java b/java/com/android/voicemail/impl/mail/Message.java new file mode 100644 index 000000000..aea5d3ead --- /dev/null +++ b/java/com/android/voicemail/impl/mail/Message.java @@ -0,0 +1,146 @@ +/* + * 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.voicemail.impl.mail; + +import android.support.annotation.VisibleForTesting; +import java.util.Date; +import java.util.HashSet; + +public abstract class Message implements Part, Body { + public static final Message[] EMPTY_ARRAY = new Message[0]; + + public static final String RECIPIENT_TYPE_TO = "to"; + public static final String RECIPIENT_TYPE_CC = "cc"; + public static final String RECIPIENT_TYPE_BCC = "bcc"; + + public enum RecipientType { + TO, + CC, + BCC, + } + + protected String mUid; + + private HashSet mFlags = null; + + protected Date mInternalDate; + + public String getUid() { + return mUid; + } + + public void setUid(String uid) { + this.mUid = uid; + } + + public abstract String getSubject() throws MessagingException; + + public abstract void setSubject(String subject) throws MessagingException; + + public Date getInternalDate() { + return mInternalDate; + } + + public void setInternalDate(Date internalDate) { + this.mInternalDate = internalDate; + } + + public abstract Date getReceivedDate() throws MessagingException; + + public abstract Date getSentDate() throws MessagingException; + + public abstract void setSentDate(Date sentDate) throws MessagingException; + + public abstract Address[] getRecipients(String type) throws MessagingException; + + public abstract void setRecipients(String type, Address[] addresses) throws MessagingException; + + public void setRecipient(String type, Address address) throws MessagingException { + setRecipients(type, new Address[] {address}); + } + + public abstract Address[] getFrom() throws MessagingException; + + public abstract void setFrom(Address from) throws MessagingException; + + public abstract Address[] getReplyTo() throws MessagingException; + + public abstract void setReplyTo(Address[] from) throws MessagingException; + + // Always use these instead of getHeader("Message-ID") or setHeader("Message-ID"); + public abstract void setMessageId(String messageId) throws MessagingException; + + public abstract String getMessageId() throws MessagingException; + + @Override + public boolean isMimeType(String mimeType) throws MessagingException { + return getContentType().startsWith(mimeType); + } + + private HashSet getFlagSet() { + if (mFlags == null) { + mFlags = new HashSet(); + } + return mFlags; + } + + /* + * TODO Refactor Flags at some point to be able to store user defined flags. + */ + public String[] getFlags() { + return getFlagSet().toArray(new String[] {}); + } + + /** + * Set/clear a flag directly, without involving overrides of {@link #setFlag} in subclasses. Only + * used for testing. + */ + @VisibleForTesting + private final void setFlagDirectlyForTest(String flag, boolean set) throws MessagingException { + if (set) { + getFlagSet().add(flag); + } else { + getFlagSet().remove(flag); + } + } + + public void setFlag(String flag, boolean set) throws MessagingException { + setFlagDirectlyForTest(flag, set); + } + + /** + * This method calls setFlag(String, boolean) + * + * @param flags + * @param set + */ + public void setFlags(String[] flags, boolean set) throws MessagingException { + for (String flag : flags) { + setFlag(flag, set); + } + } + + public boolean isSet(String flag) { + return getFlagSet().contains(flag); + } + + public abstract void saveChanges() throws MessagingException; + + @Override + public String toString() { + return getClass().getSimpleName() + ':' + mUid; + } +} diff --git a/java/com/android/voicemail/impl/mail/MessageDateComparator.java b/java/com/android/voicemail/impl/mail/MessageDateComparator.java new file mode 100644 index 000000000..89231f6c2 --- /dev/null +++ b/java/com/android/voicemail/impl/mail/MessageDateComparator.java @@ -0,0 +1,35 @@ +/* + * 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.voicemail.impl.mail; + +import java.util.Comparator; + +public class MessageDateComparator implements Comparator { + @Override + public int compare(Message o1, Message o2) { + try { + if (o1.getSentDate() == null) { + return 1; + } else if (o2.getSentDate() == null) { + return -1; + } else { + return o2.getSentDate().compareTo(o1.getSentDate()); + } + } catch (Exception e) { + return 0; + } + } +} diff --git a/java/com/android/voicemail/impl/mail/MessagingException.java b/java/com/android/voicemail/impl/mail/MessagingException.java new file mode 100644 index 000000000..c1e3051df --- /dev/null +++ b/java/com/android/voicemail/impl/mail/MessagingException.java @@ -0,0 +1,143 @@ +/* + * 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.voicemail.impl.mail; + +/** + * This exception is used for most types of failures that occur during server interactions. + * + *

Data passed through this exception should be considered non-localized. Any strings should + * either be internal-only (for debugging) or server-generated. + * + *

TO DO: Does it make sense to further collapse AuthenticationFailedException and + * CertificateValidationException and any others into this? + */ +public class MessagingException extends Exception { + public static final long serialVersionUID = -1; + + public static final int NO_ERROR = -1; + /** Any exception that does not specify a specific issue */ + public static final int UNSPECIFIED_EXCEPTION = 0; + /** Connection or IO errors */ + public static final int IOERROR = 1; + /** The configuration requested TLS but the server did not support it. */ + public static final int TLS_REQUIRED = 2; + /** Authentication is required but the server did not support it. */ + public static final int AUTH_REQUIRED = 3; + /** General security failures */ + public static final int GENERAL_SECURITY = 4; + /** Authentication failed */ + public static final int AUTHENTICATION_FAILED = 5; + /** Attempt to create duplicate account */ + public static final int DUPLICATE_ACCOUNT = 6; + /** Required security policies reported - advisory only */ + public static final int SECURITY_POLICIES_REQUIRED = 7; + /** Required security policies not supported */ + public static final int SECURITY_POLICIES_UNSUPPORTED = 8; + /** The protocol (or protocol version) isn't supported */ + public static final int PROTOCOL_VERSION_UNSUPPORTED = 9; + /** The server's SSL certificate couldn't be validated */ + public static final int CERTIFICATE_VALIDATION_ERROR = 10; + /** Authentication failed during autodiscover */ + public static final int AUTODISCOVER_AUTHENTICATION_FAILED = 11; + /** Autodiscover completed with a result (non-error) */ + public static final int AUTODISCOVER_AUTHENTICATION_RESULT = 12; + /** Ambiguous failure; server error or bad credentials */ + public static final int AUTHENTICATION_FAILED_OR_SERVER_ERROR = 13; + /** The server refused access */ + public static final int ACCESS_DENIED = 14; + /** The server refused access */ + public static final int ATTACHMENT_NOT_FOUND = 15; + /** A client SSL certificate is required for connections to the server */ + public static final int CLIENT_CERTIFICATE_REQUIRED = 16; + /** The client SSL certificate specified is invalid */ + public static final int CLIENT_CERTIFICATE_ERROR = 17; + /** The server indicates it does not support OAuth authentication */ + public static final int OAUTH_NOT_SUPPORTED = 18; + /** The server indicates it experienced an internal error */ + public static final int SERVER_ERROR = 19; + + protected int mExceptionType; + // Exception type-specific data + protected Object mExceptionData; + + public MessagingException(String message, Throwable throwable) { + this(UNSPECIFIED_EXCEPTION, message, throwable); + } + + public MessagingException(int exceptionType, String message, Throwable throwable) { + super(message, throwable); + mExceptionType = exceptionType; + mExceptionData = null; + } + + /** + * Constructs a MessagingException with an exceptionType and a null message. + * + * @param exceptionType The exception type to set for this exception. + */ + public MessagingException(int exceptionType) { + this(exceptionType, null, null); + } + + /** + * Constructs a MessagingException with a message. + * + * @param message the message for this exception + */ + public MessagingException(String message) { + this(UNSPECIFIED_EXCEPTION, message, null); + } + + /** + * Constructs a MessagingException with an exceptionType and a message. + * + * @param exceptionType The exception type to set for this exception. + */ + public MessagingException(int exceptionType, String message) { + this(exceptionType, message, null); + } + + /** + * Constructs a MessagingException with an exceptionType, a message, and data + * + * @param exceptionType The exception type to set for this exception. + * @param message the message for the exception (or null) + * @param data exception-type specific data for the exception (or null) + */ + public MessagingException(int exceptionType, String message, Object data) { + super(message); + mExceptionType = exceptionType; + mExceptionData = data; + } + + /** + * Return the exception type. Will be OTHER_EXCEPTION if not explicitly set. + * + * @return Returns the exception type. + */ + public int getExceptionType() { + return mExceptionType; + } + /** + * Return the exception data. Will be null if not explicitly set. + * + * @return Returns the exception data. + */ + public Object getExceptionData() { + return mExceptionData; + } +} diff --git a/java/com/android/voicemail/impl/mail/Multipart.java b/java/com/android/voicemail/impl/mail/Multipart.java new file mode 100644 index 000000000..e8d5046d5 --- /dev/null +++ b/java/com/android/voicemail/impl/mail/Multipart.java @@ -0,0 +1,62 @@ +/* + * 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.voicemail.impl.mail; + +import java.util.ArrayList; + +public abstract class Multipart implements Body { + protected Part mParent; + + protected ArrayList mParts = new ArrayList(); + + protected String mContentType; + + public void addBodyPart(BodyPart part) throws MessagingException { + mParts.add(part); + } + + public void addBodyPart(BodyPart part, int index) throws MessagingException { + mParts.add(index, part); + } + + public BodyPart getBodyPart(int index) throws MessagingException { + return mParts.get(index); + } + + public String getContentType() throws MessagingException { + return mContentType; + } + + public int getCount() throws MessagingException { + return mParts.size(); + } + + public boolean removeBodyPart(BodyPart part) throws MessagingException { + return mParts.remove(part); + } + + public void removeBodyPart(int index) throws MessagingException { + mParts.remove(index); + } + + public Part getParent() throws MessagingException { + return mParent; + } + + public void setParent(Part parent) throws MessagingException { + this.mParent = parent; + } +} diff --git a/java/com/android/voicemail/impl/mail/PackedString.java b/java/com/android/voicemail/impl/mail/PackedString.java new file mode 100644 index 000000000..701dab62b --- /dev/null +++ b/java/com/android/voicemail/impl/mail/PackedString.java @@ -0,0 +1,172 @@ +/* + * 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.voicemail.impl.mail; + +import android.util.ArrayMap; +import java.util.Map; + +/** + * A utility class for creating and modifying Strings that are tagged and packed together. + * + *

Uses non-printable (control chars) for internal delimiters; Intended for regular displayable + * strings only, so please use base64 or other encoding if you need to hide any binary data here. + * + *

Binary compatible with Address.pack() format, which should migrate to use this code. + */ +public class PackedString { + + /** + * Packing format is: element : [ value ] or [ value TAG-DELIMITER tag ] packed-string : [ element + * ] [ ELEMENT-DELIMITER [ element ] ]* + */ + private static final char DELIMITER_ELEMENT = '\1'; + + private static final char DELIMITER_TAG = '\2'; + + private String mString; + private ArrayMap mExploded; + private static final ArrayMap EMPTY_MAP = new ArrayMap(); + + /** + * Create a packed string using an already-packed string (e.g. from database) + * + * @param string packed string + */ + public PackedString(String string) { + mString = string; + mExploded = null; + } + + /** + * Get the value referred to by a given tag. If the tag does not exist, return null. + * + * @param tag identifier of string of interest + * @return returns value, or null if no string is found + */ + public String get(String tag) { + if (mExploded == null) { + mExploded = explode(mString); + } + return mExploded.get(tag); + } + + /** + * Return a map of all of the values referred to by a given tag. This is a shallow copy, don't + * edit the values. + * + * @return a map of the values in the packed string + */ + public Map unpack() { + if (mExploded == null) { + mExploded = explode(mString); + } + return new ArrayMap(mExploded); + } + + /** Read out all values into a map. */ + private static ArrayMap explode(String packed) { + if (packed == null || packed.length() == 0) { + return EMPTY_MAP; + } + ArrayMap map = new ArrayMap(); + + int length = packed.length(); + int elementStartIndex = 0; + int elementEndIndex = 0; + int tagEndIndex = packed.indexOf(DELIMITER_TAG); + + while (elementStartIndex < length) { + elementEndIndex = packed.indexOf(DELIMITER_ELEMENT, elementStartIndex); + if (elementEndIndex == -1) { + elementEndIndex = length; + } + String tag; + String value; + if (tagEndIndex == -1 || elementEndIndex <= tagEndIndex) { + // in this case the DELIMITER_PERSONAL is in a future pair (or not found) + // so synthesize a positional tag for the value, and don't update tagEndIndex + value = packed.substring(elementStartIndex, elementEndIndex); + tag = Integer.toString(map.size()); + } else { + value = packed.substring(elementStartIndex, tagEndIndex); + tag = packed.substring(tagEndIndex + 1, elementEndIndex); + // scan forward for next tag, if any + tagEndIndex = packed.indexOf(DELIMITER_TAG, elementEndIndex + 1); + } + map.put(tag, value); + elementStartIndex = elementEndIndex + 1; + } + + return map; + } + + /** + * Builder class for creating PackedString values. Can also be used for editing existing + * PackedString representations. + */ + public static class Builder { + ArrayMap mMap; + + /** Create a builder that's empty (for filling) */ + public Builder() { + mMap = new ArrayMap(); + } + + /** Create a builder using the values of an existing PackedString (for editing). */ + public Builder(String packed) { + mMap = explode(packed); + } + + /** + * Add a tagged value + * + * @param tag identifier of string of interest + * @param value the value to record in this position. null to delete entry. + */ + public void put(String tag, String value) { + if (value == null) { + mMap.remove(tag); + } else { + mMap.put(tag, value); + } + } + + /** + * Get the value referred to by a given tag. If the tag does not exist, return null. + * + * @param tag identifier of string of interest + * @return returns value, or null if no string is found + */ + public String get(String tag) { + return mMap.get(tag); + } + + /** Pack the values and return a single, encoded string */ + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + for (Map.Entry entry : mMap.entrySet()) { + if (sb.length() > 0) { + sb.append(DELIMITER_ELEMENT); + } + sb.append(entry.getValue()); + sb.append(DELIMITER_TAG); + sb.append(entry.getKey()); + } + return sb.toString(); + } + } +} diff --git a/java/com/android/voicemail/impl/mail/Part.java b/java/com/android/voicemail/impl/mail/Part.java new file mode 100644 index 000000000..3be5c57b9 --- /dev/null +++ b/java/com/android/voicemail/impl/mail/Part.java @@ -0,0 +1,51 @@ +/* + * 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.voicemail.impl.mail; + +import java.io.IOException; +import java.io.OutputStream; + +public interface Part extends Fetchable { + public void addHeader(String name, String value) throws MessagingException; + + public void removeHeader(String name) throws MessagingException; + + public void setHeader(String name, String value) throws MessagingException; + + public Body getBody() throws MessagingException; + + public String getContentType() throws MessagingException; + + public String getDisposition() throws MessagingException; + + public String getContentId() throws MessagingException; + + public String[] getHeader(String name) throws MessagingException; + + public void setExtendedHeader(String name, String value) throws MessagingException; + + public String getExtendedHeader(String name) throws MessagingException; + + public int getSize() throws MessagingException; + + public boolean isMimeType(String mimeType) throws MessagingException; + + public String getMimeType() throws MessagingException; + + public void setBody(Body body) throws MessagingException; + + public void writeTo(OutputStream out) throws IOException, MessagingException; +} diff --git a/java/com/android/voicemail/impl/mail/PeekableInputStream.java b/java/com/android/voicemail/impl/mail/PeekableInputStream.java new file mode 100644 index 000000000..08f867f82 --- /dev/null +++ b/java/com/android/voicemail/impl/mail/PeekableInputStream.java @@ -0,0 +1,81 @@ +/* + * 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.voicemail.impl.mail; + +import java.io.IOException; +import java.io.InputStream; + +/** + * A filtering InputStream that allows single byte "peeks" without consuming the byte. The client of + * this stream can call peek() to see the next available byte in the stream and a subsequent read + * will still return the peeked byte. + */ +public class PeekableInputStream extends InputStream { + private final InputStream mIn; + private boolean mPeeked; + private int mPeekedByte; + + public PeekableInputStream(InputStream in) { + this.mIn = in; + } + + @Override + public int read() throws IOException { + if (!mPeeked) { + return mIn.read(); + } else { + mPeeked = false; + return mPeekedByte; + } + } + + public int peek() throws IOException { + if (!mPeeked) { + mPeekedByte = read(); + mPeeked = true; + } + return mPeekedByte; + } + + @Override + public int read(byte[] b, int offset, int length) throws IOException { + if (!mPeeked) { + return mIn.read(b, offset, length); + } else { + b[0] = (byte) mPeekedByte; + mPeeked = false; + int r = mIn.read(b, offset + 1, length - 1); + if (r == -1) { + return 1; + } else { + return r + 1; + } + } + } + + @Override + public int read(byte[] b) throws IOException { + return read(b, 0, b.length); + } + + @Override + public String toString() { + return String.format( + "PeekableInputStream(in=%s, peeked=%b, peekedByte=%d)", + mIn.toString(), mPeeked, mPeekedByte); + } +} diff --git a/java/com/android/voicemail/impl/mail/TempDirectory.java b/java/com/android/voicemail/impl/mail/TempDirectory.java new file mode 100644 index 000000000..42adbeb1f --- /dev/null +++ b/java/com/android/voicemail/impl/mail/TempDirectory.java @@ -0,0 +1,40 @@ +/* + * 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.voicemail.impl.mail; + +import android.content.Context; +import java.io.File; + +/** + * TempDirectory caches the directory used for caching file. It is set up during application + * initialization. + */ +public class TempDirectory { + private static File sTempDirectory = null; + + public static void setTempDirectory(Context context) { + sTempDirectory = context.getCacheDir(); + } + + public static File getTempDirectory() { + if (sTempDirectory == null) { + throw new RuntimeException( + "TempDirectory not set. " + + "If in a unit test, call Email.setTempDirectory(context) in setUp()."); + } + return sTempDirectory; + } +} diff --git a/java/com/android/voicemail/impl/mail/internet/BinaryTempFileBody.java b/java/com/android/voicemail/impl/mail/internet/BinaryTempFileBody.java new file mode 100644 index 000000000..753b70f23 --- /dev/null +++ b/java/com/android/voicemail/impl/mail/internet/BinaryTempFileBody.java @@ -0,0 +1,87 @@ +/* + * 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.voicemail.impl.mail.internet; + +import android.util.Base64; +import android.util.Base64OutputStream; +import com.android.voicemail.impl.mail.Body; +import com.android.voicemail.impl.mail.MessagingException; +import com.android.voicemail.impl.mail.TempDirectory; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.FilterInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import org.apache.commons.io.IOUtils; + +/** + * A Body that is backed by a temp file. The Body exposes a getOutputStream method that allows the + * user to write to the temp file. After the write the body is available via getInputStream and + * writeTo one time. After writeTo is called, or the InputStream returned from getInputStream is + * closed the file is deleted and the Body should be considered disposed of. + */ +public class BinaryTempFileBody implements Body { + private File mFile; + + /** + * An alternate way to put data into a BinaryTempFileBody is to simply supply an already- created + * file. Note that this file will be deleted after it is read. + * + * @param filePath The file containing the data to be stored on disk temporarily + */ + public void setFile(String filePath) { + mFile = new File(filePath); + } + + public OutputStream getOutputStream() throws IOException { + mFile = File.createTempFile("body", null, TempDirectory.getTempDirectory()); + mFile.deleteOnExit(); + return new FileOutputStream(mFile); + } + + @Override + public InputStream getInputStream() throws MessagingException { + try { + return new BinaryTempFileBodyInputStream(new FileInputStream(mFile)); + } catch (IOException ioe) { + throw new MessagingException("Unable to open body", ioe); + } + } + + @Override + public void writeTo(OutputStream out) throws IOException, MessagingException { + InputStream in = getInputStream(); + Base64OutputStream base64Out = new Base64OutputStream(out, Base64.CRLF | Base64.NO_CLOSE); + IOUtils.copy(in, base64Out); + base64Out.close(); + mFile.delete(); + in.close(); + } + + class BinaryTempFileBodyInputStream extends FilterInputStream { + public BinaryTempFileBodyInputStream(InputStream in) { + super(in); + } + + @Override + public void close() throws IOException { + super.close(); + mFile.delete(); + } + } +} diff --git a/java/com/android/voicemail/impl/mail/internet/MimeBodyPart.java b/java/com/android/voicemail/impl/mail/internet/MimeBodyPart.java new file mode 100644 index 000000000..2add76c72 --- /dev/null +++ b/java/com/android/voicemail/impl/mail/internet/MimeBodyPart.java @@ -0,0 +1,200 @@ +/* + * 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.voicemail.impl.mail.internet; + +import com.android.voicemail.impl.mail.Body; +import com.android.voicemail.impl.mail.BodyPart; +import com.android.voicemail.impl.mail.MessagingException; +import com.android.voicemail.impl.mail.Multipart; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.util.regex.Pattern; + +/** TODO this is a close approximation of Message, need to update along with Message. */ +public class MimeBodyPart extends BodyPart { + protected MimeHeader mHeader = new MimeHeader(); + protected MimeHeader mExtendedHeader; + protected Body mBody; + protected int mSize; + + // regex that matches content id surrounded by "<>" optionally. + private static final Pattern REMOVE_OPTIONAL_BRACKETS = Pattern.compile("^]+)>?$"); + // regex that matches end of line. + private static final Pattern END_OF_LINE = Pattern.compile("\r?\n"); + + public MimeBodyPart() throws MessagingException { + this(null); + } + + public MimeBodyPart(Body body) throws MessagingException { + this(body, null); + } + + public MimeBodyPart(Body body, String mimeType) throws MessagingException { + if (mimeType != null) { + setHeader(MimeHeader.HEADER_CONTENT_TYPE, mimeType); + } + setBody(body); + } + + protected String getFirstHeader(String name) throws MessagingException { + return mHeader.getFirstHeader(name); + } + + @Override + public void addHeader(String name, String value) throws MessagingException { + mHeader.addHeader(name, value); + } + + @Override + public void setHeader(String name, String value) throws MessagingException { + mHeader.setHeader(name, value); + } + + @Override + public String[] getHeader(String name) throws MessagingException { + return mHeader.getHeader(name); + } + + @Override + public void removeHeader(String name) throws MessagingException { + mHeader.removeHeader(name); + } + + @Override + public Body getBody() throws MessagingException { + return mBody; + } + + @Override + public void setBody(Body body) throws MessagingException { + this.mBody = body; + if (body instanceof Multipart) { + Multipart multipart = + ((Multipart) body); + multipart.setParent(this); + setHeader(MimeHeader.HEADER_CONTENT_TYPE, multipart.getContentType()); + } else if (body instanceof TextBody) { + String contentType = String.format("%s;\n charset=utf-8", getMimeType()); + String name = MimeUtility.getHeaderParameter(getContentType(), "name"); + if (name != null) { + contentType += String.format(";\n name=\"%s\"", name); + } + setHeader(MimeHeader.HEADER_CONTENT_TYPE, contentType); + setHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING, "base64"); + } + } + + @Override + public String getContentType() throws MessagingException { + String contentType = getFirstHeader(MimeHeader.HEADER_CONTENT_TYPE); + if (contentType == null) { + return "text/plain"; + } else { + return contentType; + } + } + + @Override + public String getDisposition() throws MessagingException { + String contentDisposition = getFirstHeader(MimeHeader.HEADER_CONTENT_DISPOSITION); + if (contentDisposition == null) { + return null; + } else { + return contentDisposition; + } + } + + @Override + public String getContentId() throws MessagingException { + String contentId = getFirstHeader(MimeHeader.HEADER_CONTENT_ID); + if (contentId == null) { + return null; + } else { + // remove optionally surrounding brackets. + return REMOVE_OPTIONAL_BRACKETS.matcher(contentId).replaceAll("$1"); + } + } + + @Override + public String getMimeType() throws MessagingException { + return MimeUtility.getHeaderParameter(getContentType(), null); + } + + @Override + public boolean isMimeType(String mimeType) throws MessagingException { + return getMimeType().equals(mimeType); + } + + public void setSize(int size) { + this.mSize = size; + } + + @Override + public int getSize() throws MessagingException { + return mSize; + } + + /** + * Set extended header + * + * @param name Extended header name + * @param value header value - flattened by removing CR-NL if any remove header if value is null + * @throws MessagingException + */ + @Override + public void setExtendedHeader(String name, String value) throws MessagingException { + if (value == null) { + if (mExtendedHeader != null) { + mExtendedHeader.removeHeader(name); + } + return; + } + if (mExtendedHeader == null) { + mExtendedHeader = new MimeHeader(); + } + mExtendedHeader.setHeader(name, END_OF_LINE.matcher(value).replaceAll("")); + } + + /** + * Get extended header + * + * @param name Extended header name + * @return header value - null if header does not exist + * @throws MessagingException + */ + @Override + public String getExtendedHeader(String name) throws MessagingException { + if (mExtendedHeader == null) { + return null; + } + return mExtendedHeader.getFirstHeader(name); + } + + /** Write the MimeMessage out in MIME format. */ + @Override + public void writeTo(OutputStream out) throws IOException, MessagingException { + BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out), 1024); + mHeader.writeTo(out); + writer.write("\r\n"); + writer.flush(); + if (mBody != null) { + mBody.writeTo(out); + } + } +} diff --git a/java/com/android/voicemail/impl/mail/internet/MimeHeader.java b/java/com/android/voicemail/impl/mail/internet/MimeHeader.java new file mode 100644 index 000000000..d41cdb3e4 --- /dev/null +++ b/java/com/android/voicemail/impl/mail/internet/MimeHeader.java @@ -0,0 +1,158 @@ +/* + * 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.voicemail.impl.mail.internet; + +import com.android.voicemail.impl.mail.MessagingException; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.util.ArrayList; + +public class MimeHeader { + /** + * Application specific header that contains Store specific information about an attachment. In + * IMAP this contains the IMAP BODYSTRUCTURE part id so that the ImapStore can later retrieve the + * attachment at will from the server. The info is recorded from this header on + * LocalStore.appendMessage and is put back into the MIME data by LocalStore.fetch. + */ + public static final String HEADER_ANDROID_ATTACHMENT_STORE_DATA = + "X-Android-Attachment-StoreData"; + + public static final String HEADER_CONTENT_TYPE = "Content-Type"; + public static final String HEADER_CONTENT_TRANSFER_ENCODING = "Content-Transfer-Encoding"; + public static final String HEADER_CONTENT_DISPOSITION = "Content-Disposition"; + public static final String HEADER_CONTENT_ID = "Content-ID"; + + /** Fields that should be omitted when writing the header using writeTo() */ + private static final String[] WRITE_OMIT_FIELDS = { + // HEADER_ANDROID_ATTACHMENT_DOWNLOADED, + // HEADER_ANDROID_ATTACHMENT_ID, + HEADER_ANDROID_ATTACHMENT_STORE_DATA + }; + + protected final ArrayList mFields = new ArrayList(); + + public void clear() { + mFields.clear(); + } + + public String getFirstHeader(String name) throws MessagingException { + String[] header = getHeader(name); + if (header == null) { + return null; + } + return header[0]; + } + + public void addHeader(String name, String value) throws MessagingException { + mFields.add(new Field(name, value)); + } + + public void setHeader(String name, String value) throws MessagingException { + if (name == null || value == null) { + return; + } + removeHeader(name); + addHeader(name, value); + } + + public String[] getHeader(String name) throws MessagingException { + ArrayList values = new ArrayList(); + for (Field field : mFields) { + if (field.name.equalsIgnoreCase(name)) { + values.add(field.value); + } + } + if (values.size() == 0) { + return null; + } + return values.toArray(new String[] {}); + } + + public void removeHeader(String name) throws MessagingException { + ArrayList removeFields = new ArrayList(); + for (Field field : mFields) { + if (field.name.equalsIgnoreCase(name)) { + removeFields.add(field); + } + } + mFields.removeAll(removeFields); + } + + /** + * Write header into String + * + * @return CR-NL separated header string except the headers in writeOmitFields null if header is + * empty + */ + public String writeToString() { + if (mFields.size() == 0) { + return null; + } + StringBuilder builder = new StringBuilder(); + for (Field field : mFields) { + if (!arrayContains(WRITE_OMIT_FIELDS, field.name)) { + builder.append(field.name + ": " + field.value + "\r\n"); + } + } + return builder.toString(); + } + + public void writeTo(OutputStream out) throws IOException, MessagingException { + BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out), 1024); + for (Field field : mFields) { + if (!arrayContains(WRITE_OMIT_FIELDS, field.name)) { + writer.write(field.name + ": " + field.value + "\r\n"); + } + } + writer.flush(); + } + + private static class Field { + final String name; + final String value; + + public Field(String name, String value) { + this.name = name; + this.value = value; + } + + @Override + public String toString() { + return name + "=" + value; + } + } + + @Override + public String toString() { + return (mFields == null) ? null : mFields.toString(); + } + + public static final boolean arrayContains(Object[] a, Object o) { + int index = arrayIndex(a, o); + return (index >= 0); + } + + public static final int arrayIndex(Object[] a, Object o) { + for (int i = 0, count = a.length; i < count; i++) { + if (a[i].equals(o)) { + return i; + } + } + return -1; + } +} diff --git a/java/com/android/voicemail/impl/mail/internet/MimeMessage.java b/java/com/android/voicemail/impl/mail/internet/MimeMessage.java new file mode 100644 index 000000000..dfb7d7c25 --- /dev/null +++ b/java/com/android/voicemail/impl/mail/internet/MimeMessage.java @@ -0,0 +1,676 @@ +/* + * 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.voicemail.impl.mail.internet; + +import android.text.TextUtils; +import com.android.voicemail.impl.mail.Address; +import com.android.voicemail.impl.mail.Body; +import com.android.voicemail.impl.mail.BodyPart; +import com.android.voicemail.impl.mail.Message; +import com.android.voicemail.impl.mail.MessagingException; +import com.android.voicemail.impl.mail.Multipart; +import com.android.voicemail.impl.mail.Part; +import com.android.voicemail.impl.mail.utils.LogUtils; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Locale; +import java.util.Stack; +import java.util.regex.Pattern; +import org.apache.james.mime4j.BodyDescriptor; +import org.apache.james.mime4j.ContentHandler; +import org.apache.james.mime4j.EOLConvertingInputStream; +import org.apache.james.mime4j.MimeStreamParser; +import org.apache.james.mime4j.field.DateTimeField; +import org.apache.james.mime4j.field.Field; + +/** + * An implementation of Message that stores all of its metadata in RFC 822 and RFC 2045 style + * headers. + * + *

NOTE: Automatic generation of a local message-id is becoming unwieldy and should be removed. + * It would be better to simply do it explicitly on local creation of new outgoing messages. + */ +public class MimeMessage extends Message { + private MimeHeader mHeader; + private MimeHeader mExtendedHeader; + + // NOTE: The fields here are transcribed out of headers, and values stored here will supersede + // the values found in the headers. Use caution to prevent any out-of-phase errors. In + // particular, any adds/changes/deletes here must be echoed by changes in the parse() function. + private Address[] mFrom; + private Address[] mTo; + private Address[] mCc; + private Address[] mBcc; + private Address[] mReplyTo; + private Date mSentDate; + private Body mBody; + protected int mSize; + private boolean mInhibitLocalMessageId = false; + private boolean mComplete = true; + + // Shared random source for generating local message-id values + private static final java.util.Random sRandom = new java.util.Random(); + + // In MIME, en_US-like date format should be used. In other words "MMM" should be encoded to + // "Jan", not the other localized format like "Ene" (meaning January in locale es). + // This conversion is used when generating outgoing MIME messages. Incoming MIME date + // headers are parsed by org.apache.james.mime4j.field.DateTimeField which does not have any + // localization code. + private static final SimpleDateFormat DATE_FORMAT = + new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.US); + + // regex that matches content id surrounded by "<>" optionally. + private static final Pattern REMOVE_OPTIONAL_BRACKETS = Pattern.compile("^]+)>?$"); + // regex that matches end of line. + private static final Pattern END_OF_LINE = Pattern.compile("\r?\n"); + + public MimeMessage() { + mHeader = null; + } + + /** + * Generate a local message id. This is only used when none has been assigned, and is installed + * lazily. Any remote (typically server-assigned) message id takes precedence. + * + * @return a long, locally-generated message-ID value + */ + private static String generateMessageId() { + final StringBuilder sb = new StringBuilder(); + sb.append("<"); + for (int i = 0; i < 24; i++) { + // We'll use a 5-bit range (0..31) + final int value = sRandom.nextInt() & 31; + final char c = "0123456789abcdefghijklmnopqrstuv".charAt(value); + sb.append(c); + } + sb.append("."); + sb.append(Long.toString(System.currentTimeMillis())); + sb.append("@email.android.com>"); + return sb.toString(); + } + + /** + * Parse the given InputStream using Apache Mime4J to build a MimeMessage. + * + * @param in InputStream providing message content + * @throws IOException + * @throws MessagingException + */ + public MimeMessage(InputStream in) throws IOException, MessagingException { + parse(in); + } + + private MimeStreamParser init() { + // Before parsing the input stream, clear all local fields that may be superceded by + // the new incoming message. + getMimeHeaders().clear(); + mInhibitLocalMessageId = true; + mFrom = null; + mTo = null; + mCc = null; + mBcc = null; + mReplyTo = null; + mSentDate = null; + mBody = null; + + final MimeStreamParser parser = new MimeStreamParser(); + parser.setContentHandler(new MimeMessageBuilder()); + return parser; + } + + protected void parse(InputStream in) throws IOException, MessagingException { + final MimeStreamParser parser = init(); + parser.parse(new EOLConvertingInputStream(in)); + mComplete = !parser.getPrematureEof(); + } + + public void parse(InputStream in, EOLConvertingInputStream.Callback callback) + throws IOException, MessagingException { + final MimeStreamParser parser = init(); + parser.parse(new EOLConvertingInputStream(in, getSize(), callback)); + mComplete = !parser.getPrematureEof(); + } + + /** + * Return the internal mHeader value, with very lazy initialization. The goal is to save memory by + * not creating the headers until needed. + */ + private MimeHeader getMimeHeaders() { + if (mHeader == null) { + mHeader = new MimeHeader(); + } + return mHeader; + } + + @Override + public Date getReceivedDate() throws MessagingException { + return null; + } + + @Override + public Date getSentDate() throws MessagingException { + if (mSentDate == null) { + try { + DateTimeField field = + (DateTimeField) + Field.parse("Date: " + MimeUtility.unfoldAndDecode(getFirstHeader("Date"))); + mSentDate = field.getDate(); + // TODO: We should make it more clear what exceptions can be thrown here, + // and whether they reflect a normal or error condition. + } catch (Exception e) { + LogUtils.v(LogUtils.TAG, "Message missing Date header"); + } + } + if (mSentDate == null) { + // If we still don't have a date, fall back to "Delivery-date" + try { + DateTimeField field = + (DateTimeField) + Field.parse( + "Date: " + MimeUtility.unfoldAndDecode(getFirstHeader("Delivery-date"))); + mSentDate = field.getDate(); + // TODO: We should make it more clear what exceptions can be thrown here, + // and whether they reflect a normal or error condition. + } catch (Exception e) { + LogUtils.v(LogUtils.TAG, "Message also missing Delivery-Date header"); + } + } + return mSentDate; + } + + @Override + public void setSentDate(Date sentDate) throws MessagingException { + setHeader("Date", DATE_FORMAT.format(sentDate)); + this.mSentDate = sentDate; + } + + @Override + public String getContentType() throws MessagingException { + final String contentType = getFirstHeader(MimeHeader.HEADER_CONTENT_TYPE); + if (contentType == null) { + return "text/plain"; + } else { + return contentType; + } + } + + @Override + public String getDisposition() throws MessagingException { + return getFirstHeader(MimeHeader.HEADER_CONTENT_DISPOSITION); + } + + @Override + public String getContentId() throws MessagingException { + final String contentId = getFirstHeader(MimeHeader.HEADER_CONTENT_ID); + if (contentId == null) { + return null; + } else { + // remove optionally surrounding brackets. + return REMOVE_OPTIONAL_BRACKETS.matcher(contentId).replaceAll("$1"); + } + } + + public boolean isComplete() { + return mComplete; + } + + @Override + public String getMimeType() throws MessagingException { + return MimeUtility.getHeaderParameter(getContentType(), null); + } + + @Override + public int getSize() throws MessagingException { + return mSize; + } + + /** + * Returns a list of the given recipient type from this message. If no addresses are found the + * method returns an empty array. + */ + @Override + public Address[] getRecipients(String type) throws MessagingException { + if (type == RECIPIENT_TYPE_TO) { + if (mTo == null) { + mTo = Address.parse(MimeUtility.unfold(getFirstHeader("To"))); + } + return mTo; + } else if (type == RECIPIENT_TYPE_CC) { + if (mCc == null) { + mCc = Address.parse(MimeUtility.unfold(getFirstHeader("CC"))); + } + return mCc; + } else if (type == RECIPIENT_TYPE_BCC) { + if (mBcc == null) { + mBcc = Address.parse(MimeUtility.unfold(getFirstHeader("BCC"))); + } + return mBcc; + } else { + throw new MessagingException("Unrecognized recipient type."); + } + } + + @Override + public void setRecipients(String type, Address[] addresses) throws MessagingException { + final int toLength = 4; // "To: " + final int ccLength = 4; // "Cc: " + final int bccLength = 5; // "Bcc: " + if (type == RECIPIENT_TYPE_TO) { + if (addresses == null || addresses.length == 0) { + removeHeader("To"); + this.mTo = null; + } else { + setHeader("To", MimeUtility.fold(Address.toHeader(addresses), toLength)); + this.mTo = addresses; + } + } else if (type == RECIPIENT_TYPE_CC) { + if (addresses == null || addresses.length == 0) { + removeHeader("CC"); + this.mCc = null; + } else { + setHeader("CC", MimeUtility.fold(Address.toHeader(addresses), ccLength)); + this.mCc = addresses; + } + } else if (type == RECIPIENT_TYPE_BCC) { + if (addresses == null || addresses.length == 0) { + removeHeader("BCC"); + this.mBcc = null; + } else { + setHeader("BCC", MimeUtility.fold(Address.toHeader(addresses), bccLength)); + this.mBcc = addresses; + } + } else { + throw new MessagingException("Unrecognized recipient type."); + } + } + + /** Returns the unfolded, decoded value of the Subject header. */ + @Override + public String getSubject() throws MessagingException { + return MimeUtility.unfoldAndDecode(getFirstHeader("Subject")); + } + + @Override + public void setSubject(String subject) throws MessagingException { + final int headerNameLength = 9; // "Subject: " + setHeader("Subject", MimeUtility.foldAndEncode2(subject, headerNameLength)); + } + + @Override + public Address[] getFrom() throws MessagingException { + if (mFrom == null) { + String list = MimeUtility.unfold(getFirstHeader("From")); + if (list == null || list.length() == 0) { + list = MimeUtility.unfold(getFirstHeader("Sender")); + } + mFrom = Address.parse(list); + } + return mFrom; + } + + @Override + public void setFrom(Address from) throws MessagingException { + final int fromLength = 6; // "From: " + if (from != null) { + setHeader("From", MimeUtility.fold(from.toHeader(), fromLength)); + this.mFrom = new Address[] {from}; + } else { + this.mFrom = null; + } + } + + @Override + public Address[] getReplyTo() throws MessagingException { + if (mReplyTo == null) { + mReplyTo = Address.parse(MimeUtility.unfold(getFirstHeader("Reply-to"))); + } + return mReplyTo; + } + + @Override + public void setReplyTo(Address[] replyTo) throws MessagingException { + final int replyToLength = 10; // "Reply-to: " + if (replyTo == null || replyTo.length == 0) { + removeHeader("Reply-to"); + mReplyTo = null; + } else { + setHeader("Reply-to", MimeUtility.fold(Address.toHeader(replyTo), replyToLength)); + mReplyTo = replyTo; + } + } + + /** + * Set the mime "Message-ID" header + * + * @param messageId the new Message-ID value + * @throws MessagingException + */ + @Override + public void setMessageId(String messageId) throws MessagingException { + setHeader("Message-ID", messageId); + } + + /** + * Get the mime "Message-ID" header. This value will be preloaded with a locally-generated random + * ID, if the value has not previously been set. Local generation can be inhibited/ overridden by + * explicitly clearing the headers, removing the message-id header, etc. + * + * @return the Message-ID header string, or null if explicitly has been set to null + */ + @Override + public String getMessageId() throws MessagingException { + String messageId = getFirstHeader("Message-ID"); + if (messageId == null && !mInhibitLocalMessageId) { + messageId = generateMessageId(); + setMessageId(messageId); + } + return messageId; + } + + @Override + public void saveChanges() throws MessagingException { + throw new MessagingException("saveChanges not yet implemented"); + } + + @Override + public Body getBody() throws MessagingException { + return mBody; + } + + @Override + public void setBody(Body body) throws MessagingException { + this.mBody = body; + if (body instanceof Multipart) { + final Multipart multipart = ((Multipart) body); + multipart.setParent(this); + setHeader(MimeHeader.HEADER_CONTENT_TYPE, multipart.getContentType()); + setHeader("MIME-Version", "1.0"); + } else if (body instanceof TextBody) { + setHeader( + MimeHeader.HEADER_CONTENT_TYPE, String.format("%s;\n charset=utf-8", getMimeType())); + setHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING, "base64"); + } + } + + protected String getFirstHeader(String name) throws MessagingException { + return getMimeHeaders().getFirstHeader(name); + } + + @Override + public void addHeader(String name, String value) throws MessagingException { + getMimeHeaders().addHeader(name, value); + } + + @Override + public void setHeader(String name, String value) throws MessagingException { + getMimeHeaders().setHeader(name, value); + } + + @Override + public String[] getHeader(String name) throws MessagingException { + return getMimeHeaders().getHeader(name); + } + + @Override + public void removeHeader(String name) throws MessagingException { + getMimeHeaders().removeHeader(name); + if ("Message-ID".equalsIgnoreCase(name)) { + mInhibitLocalMessageId = true; + } + } + + /** + * Set extended header + * + * @param name Extended header name + * @param value header value - flattened by removing CR-NL if any remove header if value is null + * @throws MessagingException + */ + @Override + public void setExtendedHeader(String name, String value) throws MessagingException { + if (value == null) { + if (mExtendedHeader != null) { + mExtendedHeader.removeHeader(name); + } + return; + } + if (mExtendedHeader == null) { + mExtendedHeader = new MimeHeader(); + } + mExtendedHeader.setHeader(name, END_OF_LINE.matcher(value).replaceAll("")); + } + + /** + * Get extended header + * + * @param name Extended header name + * @return header value - null if header does not exist + * @throws MessagingException + */ + @Override + public String getExtendedHeader(String name) throws MessagingException { + if (mExtendedHeader == null) { + return null; + } + return mExtendedHeader.getFirstHeader(name); + } + + /** + * Set entire extended headers from String + * + * @param headers Extended header and its value - "CR-NL-separated pairs if null or empty, remove + * entire extended headers + * @throws MessagingException + */ + public void setExtendedHeaders(String headers) throws MessagingException { + if (TextUtils.isEmpty(headers)) { + mExtendedHeader = null; + } else { + mExtendedHeader = new MimeHeader(); + for (final String header : END_OF_LINE.split(headers)) { + final String[] tokens = header.split(":", 2); + if (tokens.length != 2) { + throw new MessagingException("Illegal extended headers: " + headers); + } + mExtendedHeader.setHeader(tokens[0].trim(), tokens[1].trim()); + } + } + } + + /** + * Get entire extended headers as String + * + * @return "CR-NL-separated extended headers - null if extended header does not exist + */ + public String getExtendedHeaders() { + if (mExtendedHeader != null) { + return mExtendedHeader.writeToString(); + } + return null; + } + + /** + * Write message header and body to output stream + * + * @param out Output steam to write message header and body. + */ + @Override + public void writeTo(OutputStream out) throws IOException, MessagingException { + final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out), 1024); + // Force creation of local message-id + getMessageId(); + getMimeHeaders().writeTo(out); + // mExtendedHeader will not be write out to external output stream, + // because it is intended to internal use. + writer.write("\r\n"); + writer.flush(); + if (mBody != null) { + mBody.writeTo(out); + } + } + + @Override + public InputStream getInputStream() throws MessagingException { + return null; + } + + class MimeMessageBuilder implements ContentHandler { + private final Stack stack = new Stack(); + + public MimeMessageBuilder() {} + + private void expect(Class c) { + if (!c.isInstance(stack.peek())) { + throw new IllegalStateException( + "Internal stack error: " + + "Expected '" + + c.getName() + + "' found '" + + stack.peek().getClass().getName() + + "'"); + } + } + + @Override + public void startMessage() { + if (stack.isEmpty()) { + stack.push(MimeMessage.this); + } else { + expect(Part.class); + try { + final MimeMessage m = new MimeMessage(); + ((Part) stack.peek()).setBody(m); + stack.push(m); + } catch (MessagingException me) { + throw new Error(me); + } + } + } + + @Override + public void endMessage() { + expect(MimeMessage.class); + stack.pop(); + } + + @Override + public void startHeader() { + expect(Part.class); + } + + @Override + public void field(String fieldData) { + expect(Part.class); + try { + final String[] tokens = fieldData.split(":", 2); + ((Part) stack.peek()).addHeader(tokens[0], tokens[1].trim()); + } catch (MessagingException me) { + throw new Error(me); + } + } + + @Override + public void endHeader() { + expect(Part.class); + } + + @Override + public void startMultipart(BodyDescriptor bd) { + expect(Part.class); + + final Part e = (Part) stack.peek(); + try { + final MimeMultipart multiPart = new MimeMultipart(e.getContentType()); + e.setBody(multiPart); + stack.push(multiPart); + } catch (MessagingException me) { + throw new Error(me); + } + } + + @Override + public void body(BodyDescriptor bd, InputStream in) throws IOException { + expect(Part.class); + final Body body = MimeUtility.decodeBody(in, bd.getTransferEncoding()); + try { + ((Part) stack.peek()).setBody(body); + } catch (MessagingException me) { + throw new Error(me); + } + } + + @Override + public void endMultipart() { + stack.pop(); + } + + @Override + public void startBodyPart() { + expect(MimeMultipart.class); + + try { + final MimeBodyPart bodyPart = new MimeBodyPart(); + ((MimeMultipart) stack.peek()).addBodyPart(bodyPart); + stack.push(bodyPart); + } catch (MessagingException me) { + throw new Error(me); + } + } + + @Override + public void endBodyPart() { + expect(BodyPart.class); + stack.pop(); + } + + @Override + public void epilogue(InputStream is) throws IOException { + expect(MimeMultipart.class); + final StringBuilder sb = new StringBuilder(); + int b; + while ((b = is.read()) != -1) { + sb.append((char) b); + } + // TODO: why is this commented out? + // ((Multipart) stack.peek()).setEpilogue(sb.toString()); + } + + @Override + public void preamble(InputStream is) throws IOException { + expect(MimeMultipart.class); + final StringBuilder sb = new StringBuilder(); + int b; + while ((b = is.read()) != -1) { + sb.append((char) b); + } + try { + ((MimeMultipart) stack.peek()).setPreamble(sb.toString()); + } catch (MessagingException me) { + throw new Error(me); + } + } + + @Override + public void raw(InputStream is) throws IOException { + throw new UnsupportedOperationException("Not supported"); + } + } +} diff --git a/java/com/android/voicemail/impl/mail/internet/MimeMultipart.java b/java/com/android/voicemail/impl/mail/internet/MimeMultipart.java new file mode 100644 index 000000000..87b88b52a --- /dev/null +++ b/java/com/android/voicemail/impl/mail/internet/MimeMultipart.java @@ -0,0 +1,113 @@ +/* + * 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.voicemail.impl.mail.internet; + +import com.android.voicemail.impl.mail.BodyPart; +import com.android.voicemail.impl.mail.MessagingException; +import com.android.voicemail.impl.mail.Multipart; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.OutputStreamWriter; + +public class MimeMultipart extends Multipart { + protected String mPreamble; + + protected String mContentType; + + protected String mBoundary; + + protected String mSubType; + + public MimeMultipart() throws MessagingException { + mBoundary = generateBoundary(); + setSubType("mixed"); + } + + public MimeMultipart(String contentType) throws MessagingException { + this.mContentType = contentType; + try { + mSubType = MimeUtility.getHeaderParameter(contentType, null).split("/")[1]; + mBoundary = MimeUtility.getHeaderParameter(contentType, "boundary"); + if (mBoundary == null) { + throw new MessagingException("MultiPart does not contain boundary: " + contentType); + } + } catch (Exception e) { + throw new MessagingException( + "Invalid MultiPart Content-Type; must contain subtype and boundary. (" + + contentType + + ")", + e); + } + } + + public String generateBoundary() { + StringBuffer sb = new StringBuffer(); + sb.append("----"); + for (int i = 0; i < 30; i++) { + sb.append(Integer.toString((int) (Math.random() * 35), 36)); + } + return sb.toString().toUpperCase(); + } + + public String getPreamble() throws MessagingException { + return mPreamble; + } + + public void setPreamble(String preamble) throws MessagingException { + this.mPreamble = preamble; + } + + @Override + public String getContentType() throws MessagingException { + return mContentType; + } + + public void setSubType(String subType) throws MessagingException { + this.mSubType = subType; + mContentType = String.format("multipart/%s; boundary=\"%s\"", subType, mBoundary); + } + + @Override + public void writeTo(OutputStream out) throws IOException, MessagingException { + BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out), 1024); + + if (mPreamble != null) { + writer.write(mPreamble + "\r\n"); + } + + for (int i = 0, count = mParts.size(); i < count; i++) { + BodyPart bodyPart = mParts.get(i); + writer.write("--" + mBoundary + "\r\n"); + writer.flush(); + bodyPart.writeTo(out); + writer.write("\r\n"); + } + + writer.write("--" + mBoundary + "--\r\n"); + writer.flush(); + } + + @Override + public InputStream getInputStream() throws MessagingException { + return null; + } + + public String getSubTypeForTest() { + return mSubType; + } +} diff --git a/java/com/android/voicemail/impl/mail/internet/MimeUtility.java b/java/com/android/voicemail/impl/mail/internet/MimeUtility.java new file mode 100644 index 000000000..99846027b --- /dev/null +++ b/java/com/android/voicemail/impl/mail/internet/MimeUtility.java @@ -0,0 +1,400 @@ +/* + * 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.voicemail.impl.mail.internet; + +import android.text.TextUtils; +import android.util.Base64; +import android.util.Base64DataException; +import android.util.Base64InputStream; +import com.android.voicemail.impl.VvmLog; +import com.android.voicemail.impl.mail.Body; +import com.android.voicemail.impl.mail.BodyPart; +import com.android.voicemail.impl.mail.Message; +import com.android.voicemail.impl.mail.MessagingException; +import com.android.voicemail.impl.mail.Multipart; +import com.android.voicemail.impl.mail.Part; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.ArrayList; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.apache.commons.io.IOUtils; +import org.apache.james.mime4j.codec.EncoderUtil; +import org.apache.james.mime4j.decoder.DecoderUtil; +import org.apache.james.mime4j.decoder.QuotedPrintableInputStream; +import org.apache.james.mime4j.util.CharsetUtil; + +public class MimeUtility { + private static final String LOG_TAG = "Email"; + + public static final String MIME_TYPE_RFC822 = "message/rfc822"; + private static final Pattern PATTERN_CR_OR_LF = Pattern.compile("\r|\n"); + + /** + * Replace sequences of CRLF+WSP with WSP. Tries to preserve original string object whenever + * possible. + */ + public static String unfold(String s) { + if (s == null) { + return null; + } + Matcher patternMatcher = PATTERN_CR_OR_LF.matcher(s); + if (patternMatcher.find()) { + patternMatcher.reset(); + s = patternMatcher.replaceAll(""); + } + return s; + } + + public static String decode(String s) { + if (s == null) { + return null; + } + return DecoderUtil.decodeEncodedWords(s); + } + + public static String unfoldAndDecode(String s) { + return decode(unfold(s)); + } + + // TODO implement proper foldAndEncode + // NOTE: When this really works, we *must* remove all calls to foldAndEncode2() to prevent + // duplication of encoding. + public static String foldAndEncode(String s) { + return s; + } + + /** + * INTERIM version of foldAndEncode that will be used only by Subject: headers. This is safer than + * implementing foldAndEncode() (see above) and risking unknown damage to other headers. + * + *

TODO: Copy this code to foldAndEncode(), get rid of this function, confirm all working OK. + * + * @param s original string to encode and fold + * @param usedCharacters number of characters already used up by header name + * @return the String ready to be transmitted + */ + public static String foldAndEncode2(String s, int usedCharacters) { + // james.mime4j.codec.EncoderUtil.java + // encode: encodeIfNecessary(text, usage, numUsedInHeaderName) + // Usage.TEXT_TOKENlooks like the right thing for subjects + // use WORD_ENTITY for address/names + + String encoded = EncoderUtil.encodeIfNecessary(s, EncoderUtil.Usage.TEXT_TOKEN, usedCharacters); + + return fold(encoded, usedCharacters); + } + + /** + * INTERIM: From newer version of org.apache.james (but we don't want to import the entire + * MimeUtil class). + * + *

Splits the specified string into a multiple-line representation with lines no longer than 76 + * characters (because the line might contain encoded words; see RFC 2047 section 2). If the string contains + * non-whitespace sequences longer than 76 characters a line break is inserted at the whitespace + * character following the sequence resulting in a line longer than 76 characters. + * + * @param s string to split. + * @param usedCharacters number of characters already used up. Usually the number of characters + * for header field name plus colon and one space. + * @return a multiple-line representation of the given string. + */ + public static String fold(String s, int usedCharacters) { + final int maxCharacters = 76; + + final int length = s.length(); + if (usedCharacters + length <= maxCharacters) { + return s; + } + + StringBuilder sb = new StringBuilder(); + + int lastLineBreak = -usedCharacters; + int wspIdx = indexOfWsp(s, 0); + while (true) { + if (wspIdx == length) { + sb.append(s.substring(Math.max(0, lastLineBreak))); + return sb.toString(); + } + + int nextWspIdx = indexOfWsp(s, wspIdx + 1); + + if (nextWspIdx - lastLineBreak > maxCharacters) { + sb.append(s.substring(Math.max(0, lastLineBreak), wspIdx)); + sb.append("\r\n"); + lastLineBreak = wspIdx; + } + + wspIdx = nextWspIdx; + } + } + + /** + * INTERIM: From newer version of org.apache.james (but we don't want to import the entire + * MimeUtil class). + * + *

Search for whitespace. + */ + private static int indexOfWsp(String s, int fromIndex) { + final int len = s.length(); + for (int index = fromIndex; index < len; index++) { + char c = s.charAt(index); + if (c == ' ' || c == '\t') { + return index; + } + } + return len; + } + + /** + * Returns the named parameter of a header field. If name is null the first parameter is returned, + * or if there are no additional parameters in the field the entire field is returned. Otherwise + * the named parameter is searched for in a case insensitive fashion and returned. If the + * parameter cannot be found the method returns null. + * + *

TODO: quite inefficient with the inner trimming & splitting. TODO: Also has a latent bug: + * uses "startsWith" to match the name, which can false-positive. TODO: The doc says that for a + * null name you get the first param, but you get the header. Should probably just fix the doc, + * but if other code assumes that behavior, fix the code. TODO: Need to decode %-escaped strings, + * as in: filename="ab%22d". ('+' -> ' ' conversion too? check RFC) + * + * @param header + * @param name + * @return the entire header (if name=null), the found parameter, or null + */ + public static String getHeaderParameter(String header, String name) { + if (header == null) { + return null; + } + String[] parts = unfold(header).split(";"); + if (name == null) { + return parts[0].trim(); + } + String lowerCaseName = name.toLowerCase(); + for (String part : parts) { + if (part.trim().toLowerCase().startsWith(lowerCaseName)) { + String[] parameterParts = part.split("=", 2); + if (parameterParts.length < 2) { + return null; + } + String parameter = parameterParts[1].trim(); + if (parameter.startsWith("\"") && parameter.endsWith("\"")) { + return parameter.substring(1, parameter.length() - 1); + } else { + return parameter; + } + } + } + return null; + } + + /** + * Reads the Part's body and returns a String based on any charset conversion that needed to be + * done. + * + * @param part The part containing a body + * @return a String containing the converted text in the body, or null if there was no text or an + * error during conversion. + */ + public static String getTextFromPart(Part part) { + try { + if (part != null && part.getBody() != null) { + InputStream in = part.getBody().getInputStream(); + String mimeType = part.getMimeType(); + if (mimeType != null && MimeUtility.mimeTypeMatches(mimeType, "text/*")) { + /* + * Now we read the part into a buffer for further processing. Because + * the stream is now wrapped we'll remove any transfer encoding at this point. + */ + ByteArrayOutputStream out = new ByteArrayOutputStream(); + IOUtils.copy(in, out); + in.close(); + in = null; // we want all of our memory back, and close might not release + + /* + * We've got a text part, so let's see if it needs to be processed further. + */ + String charset = getHeaderParameter(part.getContentType(), "charset"); + if (charset != null) { + /* + * See if there is conversion from the MIME charset to the Java one. + */ + charset = CharsetUtil.toJavaCharset(charset); + } + /* + * No encoding, so use us-ascii, which is the standard. + */ + if (charset == null) { + charset = "ASCII"; + } + /* + * Convert and return as new String + */ + String result = out.toString(charset); + out.close(); + return result; + } + } + + } catch (OutOfMemoryError oom) { + /* + * If we are not able to process the body there's nothing we can do about it. Return + * null and let the upper layers handle the missing content. + */ + VvmLog.e(LOG_TAG, "Unable to getTextFromPart " + oom.toString()); + } catch (Exception e) { + /* + * If we are not able to process the body there's nothing we can do about it. Return + * null and let the upper layers handle the missing content. + */ + VvmLog.e(LOG_TAG, "Unable to getTextFromPart " + e.toString()); + } + return null; + } + + /** + * Returns true if the given mimeType matches the matchAgainst specification. The comparison + * ignores case and the matchAgainst string may include "*" for a wildcard (e.g. "image/*"). + * + * @param mimeType A MIME type to check. + * @param matchAgainst A MIME type to check against. May include wildcards. + * @return true if the mimeType matches + */ + public static boolean mimeTypeMatches(String mimeType, String matchAgainst) { + Pattern p = Pattern.compile(matchAgainst.replaceAll("\\*", "\\.\\*"), Pattern.CASE_INSENSITIVE); + return p.matcher(mimeType).matches(); + } + + /** + * Returns true if the given mimeType matches any of the matchAgainst specifications. The + * comparison ignores case and the matchAgainst strings may include "*" for a wildcard (e.g. + * "image/*"). + * + * @param mimeType A MIME type to check. + * @param matchAgainst An array of MIME types to check against. May include wildcards. + * @return true if the mimeType matches any of the matchAgainst strings + */ + public static boolean mimeTypeMatches(String mimeType, String[] matchAgainst) { + for (String matchType : matchAgainst) { + if (mimeTypeMatches(mimeType, matchType)) { + return true; + } + } + return false; + } + + /** + * Given an input stream and a transfer encoding, return a wrapped input stream for that encoding + * (or the original if none is required) + * + * @param in the input stream + * @param contentTransferEncoding the content transfer encoding + * @return a properly wrapped stream + */ + public static InputStream getInputStreamForContentTransferEncoding( + InputStream in, String contentTransferEncoding) { + if (contentTransferEncoding != null) { + contentTransferEncoding = MimeUtility.getHeaderParameter(contentTransferEncoding, null); + if ("quoted-printable".equalsIgnoreCase(contentTransferEncoding)) { + in = new QuotedPrintableInputStream(in); + } else if ("base64".equalsIgnoreCase(contentTransferEncoding)) { + in = new Base64InputStream(in, Base64.DEFAULT); + } + } + return in; + } + + /** Removes any content transfer encoding from the stream and returns a Body. */ + public static Body decodeBody(InputStream in, String contentTransferEncoding) throws IOException { + /* + * We'll remove any transfer encoding by wrapping the stream. + */ + in = getInputStreamForContentTransferEncoding(in, contentTransferEncoding); + BinaryTempFileBody tempBody = new BinaryTempFileBody(); + OutputStream out = tempBody.getOutputStream(); + try { + IOUtils.copy(in, out); + } catch (Base64DataException bde) { + // TODO Need to fix this somehow + //String warning = "\n\n" + Email.getMessageDecodeErrorString(); + //out.write(warning.getBytes()); + } finally { + out.close(); + } + return tempBody; + } + + /** + * Recursively scan a Part (usually a Message) and sort out which of its children will be + * "viewable" and which will be attachments. + * + * @param part The part to be broken down + * @param viewables This arraylist will be populated with all parts that appear to be the + * "message" (e.g. text/plain & text/html) + * @param attachments This arraylist will be populated with all parts that appear to be + * attachments (including inlines) + * @throws MessagingException + */ + public static void collectParts(Part part, ArrayList viewables, ArrayList attachments) + throws MessagingException { + String disposition = part.getDisposition(); + String dispositionType = MimeUtility.getHeaderParameter(disposition, null); + // If a disposition is not specified, default to "inline" + boolean inline = + TextUtils.isEmpty(dispositionType) || "inline".equalsIgnoreCase(dispositionType); + // The lower-case mime type + String mimeType = part.getMimeType().toLowerCase(); + + if (part.getBody() instanceof Multipart) { + // If the part is Multipart but not alternative it's either mixed or + // something we don't know about, which means we treat it as mixed + // per the spec. We just process its pieces recursively. + MimeMultipart mp = (MimeMultipart) part.getBody(); + boolean foundHtml = false; + if (mp.getSubTypeForTest().equals("alternative")) { + for (int i = 0; i < mp.getCount(); i++) { + if (mp.getBodyPart(i).isMimeType("text/html")) { + foundHtml = true; + break; + } + } + } + for (int i = 0; i < mp.getCount(); i++) { + // See if we have text and html + BodyPart bp = mp.getBodyPart(i); + // If there's html, don't bother loading text + if (foundHtml && bp.isMimeType("text/plain")) { + continue; + } + collectParts(bp, viewables, attachments); + } + } else if (part.getBody() instanceof Message) { + // If the part is an embedded message we just continue to process + // it, pulling any viewables or attachments into the running list. + Message message = (Message) part.getBody(); + collectParts(message, viewables, attachments); + } else if (inline && (mimeType.startsWith("text") || (mimeType.startsWith("image")))) { + // We'll treat text and images as viewables + viewables.add(part); + } else { + // Everything else is an attachment. + attachments.add(part); + } + } +} diff --git a/java/com/android/voicemail/impl/mail/internet/TextBody.java b/java/com/android/voicemail/impl/mail/internet/TextBody.java new file mode 100644 index 000000000..dae562508 --- /dev/null +++ b/java/com/android/voicemail/impl/mail/internet/TextBody.java @@ -0,0 +1,59 @@ +/* + * 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.voicemail.impl.mail.internet; + +import android.util.Base64; +import com.android.voicemail.impl.mail.Body; +import com.android.voicemail.impl.mail.MessagingException; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.UnsupportedEncodingException; + +public class TextBody implements Body { + String mBody; + + public TextBody(String body) { + this.mBody = body; + } + + @Override + public void writeTo(OutputStream out) throws IOException, MessagingException { + byte[] bytes = mBody.getBytes("UTF-8"); + out.write(Base64.encode(bytes, Base64.CRLF)); + } + + /** + * Get the text of the body in it's unencoded format. + * + * @return + */ + public String getText() { + return mBody; + } + + /** Returns an InputStream that reads this body's text in UTF-8 format. */ + @Override + public InputStream getInputStream() throws MessagingException { + try { + byte[] b = mBody.getBytes("UTF-8"); + return new ByteArrayInputStream(b); + } catch (UnsupportedEncodingException usee) { + return null; + } + } +} diff --git a/java/com/android/voicemail/impl/mail/store/ImapConnection.java b/java/com/android/voicemail/impl/mail/store/ImapConnection.java new file mode 100644 index 000000000..0a48dfc69 --- /dev/null +++ b/java/com/android/voicemail/impl/mail/store/ImapConnection.java @@ -0,0 +1,400 @@ +/* + * 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.voicemail.impl.mail.store; + +import android.util.ArraySet; +import android.util.Base64; +import com.android.voicemail.impl.OmtpEvents; +import com.android.voicemail.impl.VvmLog; +import com.android.voicemail.impl.mail.AuthenticationFailedException; +import com.android.voicemail.impl.mail.CertificateValidationException; +import com.android.voicemail.impl.mail.MailTransport; +import com.android.voicemail.impl.mail.MessagingException; +import com.android.voicemail.impl.mail.store.ImapStore.ImapException; +import com.android.voicemail.impl.mail.store.imap.DigestMd5Utils; +import com.android.voicemail.impl.mail.store.imap.ImapConstants; +import com.android.voicemail.impl.mail.store.imap.ImapResponse; +import com.android.voicemail.impl.mail.store.imap.ImapResponseParser; +import com.android.voicemail.impl.mail.store.imap.ImapUtility; +import com.android.voicemail.impl.mail.utils.LogUtils; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.atomic.AtomicInteger; +import javax.net.ssl.SSLException; + +/** A cacheable class that stores the details for a single IMAP connection. */ +public class ImapConnection { + private final String TAG = "ImapConnection"; + + private String mLoginPhrase; + private ImapStore mImapStore; + private MailTransport mTransport; + private ImapResponseParser mParser; + private Set mCapabilities = new ArraySet<>(); + + static final String IMAP_REDACTED_LOG = "[IMAP command redacted]"; + + /** + * Next tag to use. All connections associated to the same ImapStore instance share the same + * counter to make tests simpler. (Some of the tests involve multiple connections but only have a + * single counter to track the tag.) + */ + private final AtomicInteger mNextCommandTag = new AtomicInteger(0); + + ImapConnection(ImapStore store) { + setStore(store); + } + + void setStore(ImapStore store) { + // TODO: maybe we should throw an exception if the connection is not closed here, + // if it's not currently closed, then we won't reopen it, so if the credentials have + // changed, the connection will not be reestablished. + mImapStore = store; + mLoginPhrase = null; + } + + /** + * Generates and returns the phrase to be used for authentication. This will be a LOGIN with + * username and password. + * + * @return the login command string to sent to the IMAP server + */ + String getLoginPhrase() { + if (mLoginPhrase == null) { + if (mImapStore.getUsername() != null && mImapStore.getPassword() != null) { + // build the LOGIN string once (instead of over-and-over again.) + // apply the quoting here around the built-up password + mLoginPhrase = + ImapConstants.LOGIN + + " " + + mImapStore.getUsername() + + " " + + ImapUtility.imapQuoted(mImapStore.getPassword()); + } + } + return mLoginPhrase; + } + + public void open() throws IOException, MessagingException { + if (mTransport != null && mTransport.isOpen()) { + return; + } + + try { + // copy configuration into a clean transport, if necessary + if (mTransport == null) { + mTransport = mImapStore.cloneTransport(); + } + + mTransport.open(); + + createParser(); + + // The server should greet us with something like + // * OK IMAP4rev1 Server + // consume the response before doing anything else. + ImapResponse response = mParser.readResponse(false); + if (!response.isOk()) { + mImapStore.getImapHelper().handleEvent(OmtpEvents.DATA_INVALID_INITIAL_SERVER_RESPONSE); + throw new MessagingException( + MessagingException.AUTHENTICATION_FAILED_OR_SERVER_ERROR, + "Invalid server initial response"); + } + + queryCapability(); + + maybeDoStartTls(); + + // LOGIN + doLogin(); + } catch (SSLException e) { + LogUtils.d(TAG, "SSLException ", e); + mImapStore.getImapHelper().handleEvent(OmtpEvents.DATA_SSL_EXCEPTION); + throw new CertificateValidationException(e.getMessage(), e); + } catch (IOException ioe) { + LogUtils.d(TAG, "IOException", ioe); + mImapStore.getImapHelper().handleEvent(OmtpEvents.DATA_IOE_ON_OPEN); + throw ioe; + } finally { + destroyResponses(); + } + } + + void logout() { + try { + sendCommand(ImapConstants.LOGOUT, false); + if (!mParser.readResponse(true).is(0, ImapConstants.BYE)) { + VvmLog.e(TAG, "Server did not respond LOGOUT with BYE"); + } + if (!mParser.readResponse(false).isOk()) { + VvmLog.e(TAG, "Server did not respond OK after LOGOUT"); + } + } catch (IOException | MessagingException e) { + VvmLog.e(TAG, "Error while logging out:" + e); + } + } + + /** + * Closes the connection and releases all resources. This connection can not be used again until + * {@link #setStore(ImapStore)} is called. + */ + void close() { + if (mTransport != null) { + logout(); + mTransport.close(); + mTransport = null; + } + destroyResponses(); + mParser = null; + mImapStore = null; + } + + /** Attempts to convert the connection into secure connection. */ + private void maybeDoStartTls() throws IOException, MessagingException { + // STARTTLS is required in the OMTP standard but not every implementation support it. + // Make sure the server does have this capability + if (hasCapability(ImapConstants.CAPABILITY_STARTTLS)) { + executeSimpleCommand(ImapConstants.STARTTLS); + mTransport.reopenTls(); + createParser(); + // The cached capabilities should be refreshed after TLS is established. + queryCapability(); + } + } + + /** Logs into the IMAP server */ + private void doLogin() throws IOException, MessagingException, AuthenticationFailedException { + try { + if (mCapabilities.contains(ImapConstants.CAPABILITY_AUTH_DIGEST_MD5)) { + doDigestMd5Auth(); + } else { + executeSimpleCommand(getLoginPhrase(), true); + } + } catch (ImapException ie) { + LogUtils.d(TAG, "ImapException", ie); + String status = ie.getStatus(); + String statusMessage = ie.getStatusMessage(); + String alertText = ie.getAlertText(); + + if (ImapConstants.NO.equals(status)) { + switch (statusMessage) { + case ImapConstants.NO_UNKNOWN_USER: + mImapStore.getImapHelper().handleEvent(OmtpEvents.DATA_AUTH_UNKNOWN_USER); + break; + case ImapConstants.NO_UNKNOWN_CLIENT: + mImapStore.getImapHelper().handleEvent(OmtpEvents.DATA_AUTH_UNKNOWN_DEVICE); + break; + case ImapConstants.NO_INVALID_PASSWORD: + mImapStore.getImapHelper().handleEvent(OmtpEvents.DATA_AUTH_INVALID_PASSWORD); + break; + case ImapConstants.NO_MAILBOX_NOT_INITIALIZED: + mImapStore.getImapHelper().handleEvent(OmtpEvents.DATA_AUTH_MAILBOX_NOT_INITIALIZED); + break; + case ImapConstants.NO_SERVICE_IS_NOT_PROVISIONED: + mImapStore.getImapHelper().handleEvent(OmtpEvents.DATA_AUTH_SERVICE_NOT_PROVISIONED); + break; + case ImapConstants.NO_SERVICE_IS_NOT_ACTIVATED: + mImapStore.getImapHelper().handleEvent(OmtpEvents.DATA_AUTH_SERVICE_NOT_ACTIVATED); + break; + case ImapConstants.NO_USER_IS_BLOCKED: + mImapStore.getImapHelper().handleEvent(OmtpEvents.DATA_AUTH_USER_IS_BLOCKED); + break; + case ImapConstants.NO_APPLICATION_ERROR: + mImapStore.getImapHelper().handleEvent(OmtpEvents.DATA_REJECTED_SERVER_RESPONSE); + break; + default: + mImapStore.getImapHelper().handleEvent(OmtpEvents.DATA_BAD_IMAP_CREDENTIAL); + } + throw new AuthenticationFailedException(alertText, ie); + } + + mImapStore.getImapHelper().handleEvent(OmtpEvents.DATA_REJECTED_SERVER_RESPONSE); + throw new MessagingException(alertText, ie); + } + } + + private void doDigestMd5Auth() throws IOException, MessagingException { + + // Initiate the authentication. + // The server will issue us a challenge, asking to run MD5 on the nonce with our password + // and other data, including the cnonce we randomly generated. + // + // C: a AUTHENTICATE DIGEST-MD5 + // S: (BASE64) realm="elwood.innosoft.com",nonce="OA6MG9tEQGm2hh",qop="auth", + // algorithm=md5-sess,charset=utf-8 + List responses = + executeSimpleCommand(ImapConstants.AUTHENTICATE + " " + ImapConstants.AUTH_DIGEST_MD5); + String decodedChallenge = decodeBase64(responses.get(0).getStringOrEmpty(0).getString()); + + Map challenge = DigestMd5Utils.parseDigestMessage(decodedChallenge); + DigestMd5Utils.Data data = new DigestMd5Utils.Data(mImapStore, mTransport, challenge); + + String response = data.createResponse(); + // Respond to the challenge. If the server accepts it, it will reply a response-auth which + // is the MD5 of our password and the cnonce we've provided, to prove the server does know + // the password. + // + // C: (BASE64) charset=utf-8,username="chris",realm="elwood.innosoft.com", + // nonce="OA6MG9tEQGm2hh",nc=00000001,cnonce="OA6MHXh6VqTrRk", + // digest-uri="imap/elwood.innosoft.com", + // response=d388dad90d4bbd760a152321f2143af7,qop=auth + // S: (BASE64) rspauth=ea40f60335c427b5527b84dbabcdfffd + + responses = executeContinuationResponse(encodeBase64(response), true); + + // Verify response-auth. + // If failed verifyResponseAuth() will throw a MessagingException, terminating the + // connection + String decodedResponseAuth = decodeBase64(responses.get(0).getStringOrEmpty(0).getString()); + data.verifyResponseAuth(decodedResponseAuth); + + // Send a empty response to indicate we've accepted the response-auth + // + // C: (empty) + // S: a OK User logged in + executeContinuationResponse("", false); + } + + private static String decodeBase64(String string) { + return new String(Base64.decode(string, Base64.DEFAULT)); + } + + private static String encodeBase64(String string) { + return Base64.encodeToString(string.getBytes(), Base64.NO_WRAP); + } + + private void queryCapability() throws IOException, MessagingException { + List responses = executeSimpleCommand(ImapConstants.CAPABILITY); + mCapabilities.clear(); + Set disabledCapabilities = + mImapStore.getImapHelper().getConfig().getDisabledCapabilities(); + for (ImapResponse response : responses) { + if (response.isTagged()) { + continue; + } + for (int i = 0; i < response.size(); i++) { + String capability = response.getStringOrEmpty(i).getString(); + if (disabledCapabilities != null) { + if (!disabledCapabilities.contains(capability)) { + mCapabilities.add(capability); + } + } else { + mCapabilities.add(capability); + } + } + } + + LogUtils.d(TAG, "Capabilities: " + mCapabilities.toString()); + } + + private boolean hasCapability(String capability) { + return mCapabilities.contains(capability); + } + /** + * Create an {@link ImapResponseParser} from {@code mTransport.getInputStream()} and set it to + * {@link #mParser}. + * + *

If we already have an {@link ImapResponseParser}, we {@link #destroyResponses()} and throw + * it away. + */ + private void createParser() { + destroyResponses(); + mParser = new ImapResponseParser(mTransport.getInputStream()); + } + + public void destroyResponses() { + if (mParser != null) { + mParser.destroyResponses(); + } + } + + public ImapResponse readResponse() throws IOException, MessagingException { + return mParser.readResponse(false); + } + + public List executeSimpleCommand(String command) + throws IOException, MessagingException { + return executeSimpleCommand(command, false); + } + + /** + * Send a single command to the server. The command will be preceded by an IMAP command tag and + * followed by \r\n (caller need not supply them). Execute a simple command at the server, a + * simple command being one that is sent in a single line of text + * + * @param command the command to send to the server + * @param sensitive whether the command should be redacted in logs (used for login) + * @return a list of ImapResponses + * @throws IOException + * @throws MessagingException + */ + public List executeSimpleCommand(String command, boolean sensitive) + throws IOException, MessagingException { + // TODO: It may be nice to catch IOExceptions and close the connection here. + // Currently, we expect callers to do that, but if they fail to we'll be in a broken state. + sendCommand(command, sensitive); + return getCommandResponses(); + } + + public String sendCommand(String command, boolean sensitive) + throws IOException, MessagingException { + open(); + + if (mTransport == null) { + throw new IOException("Null transport"); + } + String tag = Integer.toString(mNextCommandTag.incrementAndGet()); + String commandToSend = tag + " " + command; + mTransport.writeLine(commandToSend, (sensitive ? IMAP_REDACTED_LOG : command)); + return tag; + } + + List executeContinuationResponse(String response, boolean sensitive) + throws IOException, MessagingException { + mTransport.writeLine(response, (sensitive ? IMAP_REDACTED_LOG : response)); + return getCommandResponses(); + } + + /** + * Read and return all of the responses from the most recent command sent to the server + * + * @return a list of ImapResponses + * @throws IOException + * @throws MessagingException + */ + List getCommandResponses() throws IOException, MessagingException { + final List responses = new ArrayList(); + ImapResponse response; + do { + response = mParser.readResponse(false); + responses.add(response); + } while (!(response.isTagged() || response.isContinuationRequest())); + + if (!(response.isOk() || response.isContinuationRequest())) { + final String toString = response.toString(); + final String status = response.getStatusOrEmpty().getString(); + final String statusMessage = response.getStatusResponseTextOrEmpty().getString(); + final String alert = response.getAlertTextOrEmpty().getString(); + final String responseCode = response.getResponseCodeOrEmpty().getString(); + destroyResponses(); + throw new ImapException(toString, status, statusMessage, alert, responseCode); + } + return responses; + } +} diff --git a/java/com/android/voicemail/impl/mail/store/ImapFolder.java b/java/com/android/voicemail/impl/mail/store/ImapFolder.java new file mode 100644 index 000000000..1d9b01120 --- /dev/null +++ b/java/com/android/voicemail/impl/mail/store/ImapFolder.java @@ -0,0 +1,797 @@ +/* + * 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.voicemail.impl.mail.store; + +import android.content.Context; +import android.support.annotation.Nullable; +import android.support.annotation.VisibleForTesting; +import android.text.TextUtils; +import android.util.ArrayMap; +import android.util.Base64DataException; +import com.android.voicemail.impl.OmtpEvents; +import com.android.voicemail.impl.mail.AuthenticationFailedException; +import com.android.voicemail.impl.mail.Body; +import com.android.voicemail.impl.mail.FetchProfile; +import com.android.voicemail.impl.mail.Flag; +import com.android.voicemail.impl.mail.Message; +import com.android.voicemail.impl.mail.MessagingException; +import com.android.voicemail.impl.mail.Part; +import com.android.voicemail.impl.mail.internet.BinaryTempFileBody; +import com.android.voicemail.impl.mail.internet.MimeBodyPart; +import com.android.voicemail.impl.mail.internet.MimeHeader; +import com.android.voicemail.impl.mail.internet.MimeMultipart; +import com.android.voicemail.impl.mail.internet.MimeUtility; +import com.android.voicemail.impl.mail.store.ImapStore.ImapException; +import com.android.voicemail.impl.mail.store.ImapStore.ImapMessage; +import com.android.voicemail.impl.mail.store.imap.ImapConstants; +import com.android.voicemail.impl.mail.store.imap.ImapElement; +import com.android.voicemail.impl.mail.store.imap.ImapList; +import com.android.voicemail.impl.mail.store.imap.ImapResponse; +import com.android.voicemail.impl.mail.store.imap.ImapString; +import com.android.voicemail.impl.mail.utils.LogUtils; +import com.android.voicemail.impl.mail.utils.Utility; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.ArrayList; +import java.util.Date; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Locale; + +public class ImapFolder { + private static final String TAG = "ImapFolder"; + private static final String[] PERMANENT_FLAGS = { + Flag.DELETED, Flag.SEEN, Flag.FLAGGED, Flag.ANSWERED + }; + private static final int COPY_BUFFER_SIZE = 16 * 1024; + + private final ImapStore mStore; + private final String mName; + private int mMessageCount = -1; + private ImapConnection mConnection; + private String mMode; + private boolean mExists; + /** A set of hashes that can be used to track dirtiness */ + Object mHash[]; + + public static final String MODE_READ_ONLY = "mode_read_only"; + public static final String MODE_READ_WRITE = "mode_read_write"; + + public ImapFolder(ImapStore store, String name) { + mStore = store; + mName = name; + } + + /** Callback for each message retrieval. */ + public interface MessageRetrievalListener { + public void messageRetrieved(Message message); + } + + private void destroyResponses() { + if (mConnection != null) { + mConnection.destroyResponses(); + } + } + + public void open(String mode) throws MessagingException { + try { + if (isOpen()) { + throw new AssertionError("Duplicated open on ImapFolder"); + } + synchronized (this) { + mConnection = mStore.getConnection(); + } + // * FLAGS (\Answered \Flagged \Deleted \Seen \Draft NonJunk + // $MDNSent) + // * OK [PERMANENTFLAGS (\Answered \Flagged \Deleted \Seen \Draft + // NonJunk $MDNSent \*)] Flags permitted. + // * 23 EXISTS + // * 0 RECENT + // * OK [UIDVALIDITY 1125022061] UIDs valid + // * OK [UIDNEXT 57576] Predicted next UID + // 2 OK [READ-WRITE] Select completed. + try { + doSelect(); + } catch (IOException ioe) { + throw ioExceptionHandler(mConnection, ioe); + } finally { + destroyResponses(); + } + } catch (AuthenticationFailedException e) { + // Don't cache this connection, so we're forced to try connecting/login again + mConnection = null; + close(false); + throw e; + } catch (MessagingException e) { + mExists = false; + close(false); + throw e; + } + } + + public boolean isOpen() { + return mExists && mConnection != null; + } + + public String getMode() { + return mMode; + } + + public void close(boolean expunge) { + if (expunge) { + try { + expunge(); + } catch (MessagingException e) { + LogUtils.e(TAG, e, "Messaging Exception"); + } + } + mMessageCount = -1; + synchronized (this) { + mConnection = null; + } + } + + public int getMessageCount() { + return mMessageCount; + } + + String[] getSearchUids(List responses) { + // S: * SEARCH 2 3 6 + final ArrayList uids = new ArrayList(); + for (ImapResponse response : responses) { + if (!response.isDataResponse(0, ImapConstants.SEARCH)) { + continue; + } + // Found SEARCH response data + for (int i = 1; i < response.size(); i++) { + ImapString s = response.getStringOrEmpty(i); + if (s.isString()) { + uids.add(s.getString()); + } + } + } + return uids.toArray(Utility.EMPTY_STRINGS); + } + + @VisibleForTesting + String[] searchForUids(String searchCriteria) throws MessagingException { + checkOpen(); + try { + try { + final String command = ImapConstants.UID_SEARCH + " " + searchCriteria; + final String[] result = getSearchUids(mConnection.executeSimpleCommand(command)); + LogUtils.d(TAG, "searchForUids '" + searchCriteria + "' results: " + result.length); + return result; + } catch (ImapException me) { + LogUtils.d(TAG, "ImapException in search: " + searchCriteria, me); + return Utility.EMPTY_STRINGS; // Not found + } catch (IOException ioe) { + LogUtils.d(TAG, "IOException in search: " + searchCriteria, ioe); + mStore.getImapHelper().handleEvent(OmtpEvents.DATA_GENERIC_IMAP_IOE); + throw ioExceptionHandler(mConnection, ioe); + } + } finally { + destroyResponses(); + } + } + + @Nullable + public Message getMessage(String uid) throws MessagingException { + checkOpen(); + + final String[] uids = searchForUids(ImapConstants.UID + " " + uid); + for (int i = 0; i < uids.length; i++) { + if (uids[i].equals(uid)) { + return new ImapMessage(uid, this); + } + } + LogUtils.e(TAG, "UID " + uid + " not found on server"); + return null; + } + + @VisibleForTesting + protected static boolean isAsciiString(String str) { + int len = str.length(); + for (int i = 0; i < len; i++) { + char c = str.charAt(i); + if (c >= 128) return false; + } + return true; + } + + public Message[] getMessages(String[] uids) throws MessagingException { + if (uids == null) { + uids = searchForUids("1:* NOT DELETED"); + } + return getMessagesInternal(uids); + } + + public Message[] getMessagesInternal(String[] uids) { + final ArrayList messages = new ArrayList(uids.length); + for (int i = 0; i < uids.length; i++) { + final String uid = uids[i]; + final ImapMessage message = new ImapMessage(uid, this); + messages.add(message); + } + return messages.toArray(Message.EMPTY_ARRAY); + } + + public void fetch(Message[] messages, FetchProfile fp, MessageRetrievalListener listener) + throws MessagingException { + try { + fetchInternal(messages, fp, listener); + } catch (RuntimeException e) { // Probably a parser error. + LogUtils.w(TAG, "Exception detected: " + e.getMessage()); + throw e; + } + } + + public void fetchInternal(Message[] messages, FetchProfile fp, MessageRetrievalListener listener) + throws MessagingException { + if (messages.length == 0) { + return; + } + checkOpen(); + ArrayMap messageMap = new ArrayMap(); + for (Message m : messages) { + messageMap.put(m.getUid(), m); + } + + /* + * Figure out what command we are going to run: + * FLAGS - UID FETCH (FLAGS) + * ENVELOPE - UID FETCH (INTERNALDATE UID RFC822.SIZE FLAGS BODY.PEEK[ + * HEADER.FIELDS (date subject from content-type to cc)]) + * STRUCTURE - UID FETCH (BODYSTRUCTURE) + * BODY_SANE - UID FETCH (BODY.PEEK[]<0.N>) where N = max bytes returned + * BODY - UID FETCH (BODY.PEEK[]) + * Part - UID FETCH (BODY.PEEK[ID]) where ID = mime part ID + */ + + final LinkedHashSet fetchFields = new LinkedHashSet(); + + fetchFields.add(ImapConstants.UID); + if (fp.contains(FetchProfile.Item.FLAGS)) { + fetchFields.add(ImapConstants.FLAGS); + } + if (fp.contains(FetchProfile.Item.ENVELOPE)) { + fetchFields.add(ImapConstants.INTERNALDATE); + fetchFields.add(ImapConstants.RFC822_SIZE); + fetchFields.add(ImapConstants.FETCH_FIELD_HEADERS); + } + if (fp.contains(FetchProfile.Item.STRUCTURE)) { + fetchFields.add(ImapConstants.BODYSTRUCTURE); + } + + if (fp.contains(FetchProfile.Item.BODY_SANE)) { + fetchFields.add(ImapConstants.FETCH_FIELD_BODY_PEEK_SANE); + } + if (fp.contains(FetchProfile.Item.BODY)) { + fetchFields.add(ImapConstants.FETCH_FIELD_BODY_PEEK); + } + + // TODO Why are we only fetching the first part given? + final Part fetchPart = fp.getFirstPart(); + if (fetchPart != null) { + final String[] partIds = fetchPart.getHeader(MimeHeader.HEADER_ANDROID_ATTACHMENT_STORE_DATA); + // TODO Why can a single part have more than one Id? And why should we only fetch + // the first id if there are more than one? + if (partIds != null) { + fetchFields.add(ImapConstants.FETCH_FIELD_BODY_PEEK_BARE + "[" + partIds[0] + "]"); + } + } + + try { + mConnection.sendCommand( + String.format( + Locale.US, + ImapConstants.UID_FETCH + " %s (%s)", + ImapStore.joinMessageUids(messages), + Utility.combine(fetchFields.toArray(new String[fetchFields.size()]), ' ')), + false); + ImapResponse response; + do { + response = null; + try { + response = mConnection.readResponse(); + + if (!response.isDataResponse(1, ImapConstants.FETCH)) { + continue; // Ignore + } + final ImapList fetchList = response.getListOrEmpty(2); + final String uid = fetchList.getKeyedStringOrEmpty(ImapConstants.UID).getString(); + if (TextUtils.isEmpty(uid)) continue; + + ImapMessage message = (ImapMessage) messageMap.get(uid); + if (message == null) continue; + + if (fp.contains(FetchProfile.Item.FLAGS)) { + final ImapList flags = fetchList.getKeyedListOrEmpty(ImapConstants.FLAGS); + for (int i = 0, count = flags.size(); i < count; i++) { + final ImapString flag = flags.getStringOrEmpty(i); + if (flag.is(ImapConstants.FLAG_DELETED)) { + message.setFlagInternal(Flag.DELETED, true); + } else if (flag.is(ImapConstants.FLAG_ANSWERED)) { + message.setFlagInternal(Flag.ANSWERED, true); + } else if (flag.is(ImapConstants.FLAG_SEEN)) { + message.setFlagInternal(Flag.SEEN, true); + } else if (flag.is(ImapConstants.FLAG_FLAGGED)) { + message.setFlagInternal(Flag.FLAGGED, true); + } + } + } + if (fp.contains(FetchProfile.Item.ENVELOPE)) { + final Date internalDate = + fetchList.getKeyedStringOrEmpty(ImapConstants.INTERNALDATE).getDateOrNull(); + final int size = + fetchList.getKeyedStringOrEmpty(ImapConstants.RFC822_SIZE).getNumberOrZero(); + final String header = + fetchList + .getKeyedStringOrEmpty(ImapConstants.BODY_BRACKET_HEADER, true) + .getString(); + + message.setInternalDate(internalDate); + message.setSize(size); + message.parse(Utility.streamFromAsciiString(header)); + } + if (fp.contains(FetchProfile.Item.STRUCTURE)) { + ImapList bs = fetchList.getKeyedListOrEmpty(ImapConstants.BODYSTRUCTURE); + if (!bs.isEmpty()) { + try { + parseBodyStructure(bs, message, ImapConstants.TEXT); + } catch (MessagingException e) { + LogUtils.v(TAG, e, "Error handling message"); + message.setBody(null); + } + } + } + if (fp.contains(FetchProfile.Item.BODY) || fp.contains(FetchProfile.Item.BODY_SANE)) { + // Body is keyed by "BODY[]...". + // Previously used "BODY[..." but this can be confused with "BODY[HEADER..." + // TODO Should we accept "RFC822" as well?? + ImapString body = fetchList.getKeyedStringOrEmpty("BODY[]", true); + InputStream bodyStream = body.getAsStream(); + message.parse(bodyStream); + } + if (fetchPart != null) { + InputStream bodyStream = fetchList.getKeyedStringOrEmpty("BODY[", true).getAsStream(); + String encodings[] = fetchPart.getHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING); + + String contentTransferEncoding = null; + if (encodings != null && encodings.length > 0) { + contentTransferEncoding = encodings[0]; + } else { + // According to http://tools.ietf.org/html/rfc2045#section-6.1 + // "7bit" is the default. + contentTransferEncoding = "7bit"; + } + + try { + // TODO Don't create 2 temp files. + // decodeBody creates BinaryTempFileBody, but we could avoid this + // if we implement ImapStringBody. + // (We'll need to share a temp file. Protect it with a ref-count.) + message.setBody( + decodeBody( + mStore.getContext(), + bodyStream, + contentTransferEncoding, + fetchPart.getSize(), + listener)); + } catch (Exception e) { + // TODO: Figure out what kinds of exceptions might actually be thrown + // from here. This blanket catch-all is because we're not sure what to + // do if we don't have a contentTransferEncoding, and we don't have + // time to figure out what exceptions might be thrown. + LogUtils.e(TAG, "Error fetching body %s", e); + } + } + + if (listener != null) { + listener.messageRetrieved(message); + } + } finally { + destroyResponses(); + } + } while (!response.isTagged()); + } catch (IOException ioe) { + mStore.getImapHelper().handleEvent(OmtpEvents.DATA_GENERIC_IMAP_IOE); + throw ioExceptionHandler(mConnection, ioe); + } + } + + /** + * Removes any content transfer encoding from the stream and returns a Body. This code is + * taken/condensed from MimeUtility.decodeBody + */ + private static Body decodeBody( + Context context, + InputStream in, + String contentTransferEncoding, + int size, + MessageRetrievalListener listener) + throws IOException { + // Get a properly wrapped input stream + in = MimeUtility.getInputStreamForContentTransferEncoding(in, contentTransferEncoding); + BinaryTempFileBody tempBody = new BinaryTempFileBody(); + OutputStream out = tempBody.getOutputStream(); + try { + byte[] buffer = new byte[COPY_BUFFER_SIZE]; + int n = 0; + int count = 0; + while (-1 != (n = in.read(buffer))) { + out.write(buffer, 0, n); + count += n; + } + } catch (Base64DataException bde) { + String warning = "\n\nThere was an error while decoding the message."; + out.write(warning.getBytes()); + } finally { + out.close(); + } + return tempBody; + } + + public String[] getPermanentFlags() { + return PERMANENT_FLAGS; + } + + /** + * Handle any untagged responses that the caller doesn't care to handle themselves. + * + * @param responses + */ + private void handleUntaggedResponses(List responses) { + for (ImapResponse response : responses) { + handleUntaggedResponse(response); + } + } + + /** + * Handle an untagged response that the caller doesn't care to handle themselves. + * + * @param response + */ + private void handleUntaggedResponse(ImapResponse response) { + if (response.isDataResponse(1, ImapConstants.EXISTS)) { + mMessageCount = response.getStringOrEmpty(0).getNumberOrZero(); + } + } + + private static void parseBodyStructure(ImapList bs, Part part, String id) + throws MessagingException { + if (bs.getElementOrNone(0).isList()) { + /* + * This is a multipart/* + */ + MimeMultipart mp = new MimeMultipart(); + for (int i = 0, count = bs.size(); i < count; i++) { + ImapElement e = bs.getElementOrNone(i); + if (e.isList()) { + /* + * For each part in the message we're going to add a new BodyPart and parse + * into it. + */ + MimeBodyPart bp = new MimeBodyPart(); + if (id.equals(ImapConstants.TEXT)) { + parseBodyStructure(bs.getListOrEmpty(i), bp, Integer.toString(i + 1)); + + } else { + parseBodyStructure(bs.getListOrEmpty(i), bp, id + "." + (i + 1)); + } + mp.addBodyPart(bp); + + } else { + if (e.isString()) { + mp.setSubType(bs.getStringOrEmpty(i).getString().toLowerCase(Locale.US)); + } + break; // Ignore the rest of the list. + } + } + part.setBody(mp); + } else { + /* + * This is a body. We need to add as much information as we can find out about + * it to the Part. + */ + + /* + body type + body subtype + body parameter parenthesized list + body id + body description + body encoding + body size + */ + + final ImapString type = bs.getStringOrEmpty(0); + final ImapString subType = bs.getStringOrEmpty(1); + final String mimeType = (type.getString() + "/" + subType.getString()).toLowerCase(Locale.US); + + final ImapList bodyParams = bs.getListOrEmpty(2); + final ImapString cid = bs.getStringOrEmpty(3); + final ImapString encoding = bs.getStringOrEmpty(5); + final int size = bs.getStringOrEmpty(6).getNumberOrZero(); + + if (MimeUtility.mimeTypeMatches(mimeType, MimeUtility.MIME_TYPE_RFC822)) { + // A body type of type MESSAGE and subtype RFC822 + // contains, immediately after the basic fields, the + // envelope structure, body structure, and size in + // text lines of the encapsulated message. + // [MESSAGE, RFC822, [NAME, filename.eml], NIL, NIL, 7BIT, 5974, NIL, + // [INLINE, [FILENAME*0, Fwd: Xxx..., FILENAME*1, filename.eml]], NIL] + /* + * This will be caught by fetch and handled appropriately. + */ + throw new MessagingException( + "BODYSTRUCTURE " + MimeUtility.MIME_TYPE_RFC822 + " not yet supported."); + } + + /* + * Set the content type with as much information as we know right now. + */ + final StringBuilder contentType = new StringBuilder(mimeType); + + /* + * If there are body params we might be able to get some more information out + * of them. + */ + for (int i = 1, count = bodyParams.size(); i < count; i += 2) { + + // TODO We need to convert " into %22, but + // because MimeUtility.getHeaderParameter doesn't recognize it, + // we can't fix it for now. + contentType.append( + String.format( + ";\n %s=\"%s\"", + bodyParams.getStringOrEmpty(i - 1).getString(), + bodyParams.getStringOrEmpty(i).getString())); + } + + part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, contentType.toString()); + + // Extension items + final ImapList bodyDisposition; + + if (type.is(ImapConstants.TEXT) && bs.getElementOrNone(9).isList()) { + // If media-type is TEXT, 9th element might be: [body-fld-lines] := number + // So, if it's not a list, use 10th element. + // (Couldn't find evidence in the RFC if it's ALWAYS 10th element.) + bodyDisposition = bs.getListOrEmpty(9); + } else { + bodyDisposition = bs.getListOrEmpty(8); + } + + final StringBuilder contentDisposition = new StringBuilder(); + + if (bodyDisposition.size() > 0) { + final String bodyDisposition0Str = + bodyDisposition.getStringOrEmpty(0).getString().toLowerCase(Locale.US); + if (!TextUtils.isEmpty(bodyDisposition0Str)) { + contentDisposition.append(bodyDisposition0Str); + } + + final ImapList bodyDispositionParams = bodyDisposition.getListOrEmpty(1); + if (!bodyDispositionParams.isEmpty()) { + /* + * If there is body disposition information we can pull some more + * information about the attachment out. + */ + for (int i = 1, count = bodyDispositionParams.size(); i < count; i += 2) { + + // TODO We need to convert " into %22. See above. + contentDisposition.append( + String.format( + Locale.US, + ";\n %s=\"%s\"", + bodyDispositionParams + .getStringOrEmpty(i - 1) + .getString() + .toLowerCase(Locale.US), + bodyDispositionParams.getStringOrEmpty(i).getString())); + } + } + } + + if ((size > 0) + && (MimeUtility.getHeaderParameter(contentDisposition.toString(), "size") == null)) { + contentDisposition.append(String.format(Locale.US, ";\n size=%d", size)); + } + + if (contentDisposition.length() > 0) { + /* + * Set the content disposition containing at least the size. Attachment + * handling code will use this down the road. + */ + part.setHeader(MimeHeader.HEADER_CONTENT_DISPOSITION, contentDisposition.toString()); + } + + /* + * Set the Content-Transfer-Encoding header. Attachment code will use this + * to parse the body. + */ + if (!encoding.isEmpty()) { + part.setHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING, encoding.getString()); + } + + /* + * Set the Content-ID header. + */ + if (!cid.isEmpty()) { + part.setHeader(MimeHeader.HEADER_CONTENT_ID, cid.getString()); + } + + if (size > 0) { + if (part instanceof ImapMessage) { + ((ImapMessage) part).setSize(size); + } else if (part instanceof MimeBodyPart) { + ((MimeBodyPart) part).setSize(size); + } else { + throw new MessagingException("Unknown part type " + part.toString()); + } + } + part.setHeader(MimeHeader.HEADER_ANDROID_ATTACHMENT_STORE_DATA, id); + } + } + + public Message[] expunge() throws MessagingException { + checkOpen(); + try { + handleUntaggedResponses(mConnection.executeSimpleCommand(ImapConstants.EXPUNGE)); + } catch (IOException ioe) { + mStore.getImapHelper().handleEvent(OmtpEvents.DATA_GENERIC_IMAP_IOE); + throw ioExceptionHandler(mConnection, ioe); + } finally { + destroyResponses(); + } + return null; + } + + public void setFlags(Message[] messages, String[] flags, boolean value) + throws MessagingException { + checkOpen(); + + String allFlags = ""; + if (flags.length > 0) { + StringBuilder flagList = new StringBuilder(); + for (int i = 0, count = flags.length; i < count; i++) { + String flag = flags[i]; + if (flag == Flag.SEEN) { + flagList.append(" " + ImapConstants.FLAG_SEEN); + } else if (flag == Flag.DELETED) { + flagList.append(" " + ImapConstants.FLAG_DELETED); + } else if (flag == Flag.FLAGGED) { + flagList.append(" " + ImapConstants.FLAG_FLAGGED); + } else if (flag == Flag.ANSWERED) { + flagList.append(" " + ImapConstants.FLAG_ANSWERED); + } + } + allFlags = flagList.substring(1); + } + try { + mConnection.executeSimpleCommand( + String.format( + Locale.US, + ImapConstants.UID_STORE + " %s %s" + ImapConstants.FLAGS_SILENT + " (%s)", + ImapStore.joinMessageUids(messages), + value ? "+" : "-", + allFlags)); + + } catch (IOException ioe) { + mStore.getImapHelper().handleEvent(OmtpEvents.DATA_GENERIC_IMAP_IOE); + throw ioExceptionHandler(mConnection, ioe); + } finally { + destroyResponses(); + } + } + + /** + * Selects the folder for use. Before performing any operations on this folder, it must be + * selected. + */ + private void doSelect() throws IOException, MessagingException { + final List responses = + mConnection.executeSimpleCommand( + String.format(Locale.US, ImapConstants.SELECT + " \"%s\"", mName)); + + // Assume the folder is opened read-write; unless we are notified otherwise + mMode = MODE_READ_WRITE; + int messageCount = -1; + for (ImapResponse response : responses) { + if (response.isDataResponse(1, ImapConstants.EXISTS)) { + messageCount = response.getStringOrEmpty(0).getNumberOrZero(); + } else if (response.isOk()) { + final ImapString responseCode = response.getResponseCodeOrEmpty(); + if (responseCode.is(ImapConstants.READ_ONLY)) { + mMode = MODE_READ_ONLY; + } else if (responseCode.is(ImapConstants.READ_WRITE)) { + mMode = MODE_READ_WRITE; + } + } else if (response.isTagged()) { // Not OK + mStore.getImapHelper().handleEvent(OmtpEvents.DATA_MAILBOX_OPEN_FAILED); + throw new MessagingException( + "Can't open mailbox: " + response.getStatusResponseTextOrEmpty()); + } + } + if (messageCount == -1) { + throw new MessagingException("Did not find message count during select"); + } + mMessageCount = messageCount; + mExists = true; + } + + public class Quota { + + public final int occupied; + public final int total; + + public Quota(int occupied, int total) { + this.occupied = occupied; + this.total = total; + } + } + + public Quota getQuota() throws MessagingException { + try { + final List responses = + mConnection.executeSimpleCommand( + String.format(Locale.US, ImapConstants.GETQUOTAROOT + " \"%s\"", mName)); + + for (ImapResponse response : responses) { + if (!response.isDataResponse(0, ImapConstants.QUOTA)) { + continue; + } + ImapList list = response.getListOrEmpty(2); + for (int i = 0; i < list.size(); i += 3) { + if (!list.getStringOrEmpty(i).is("voice")) { + continue; + } + return new Quota( + list.getStringOrEmpty(i + 1).getNumber(-1), + list.getStringOrEmpty(i + 2).getNumber(-1)); + } + } + } catch (IOException ioe) { + mStore.getImapHelper().handleEvent(OmtpEvents.DATA_GENERIC_IMAP_IOE); + throw ioExceptionHandler(mConnection, ioe); + } finally { + destroyResponses(); + } + return null; + } + + private void checkOpen() throws MessagingException { + if (!isOpen()) { + throw new MessagingException("Folder " + mName + " is not open."); + } + } + + private MessagingException ioExceptionHandler(ImapConnection connection, IOException ioe) { + LogUtils.d(TAG, "IO Exception detected: ", ioe); + connection.close(); + if (connection == mConnection) { + mConnection = null; // To prevent close() from returning the connection to the pool. + close(false); + } + return new MessagingException(MessagingException.IOERROR, "IO Error", ioe); + } + + public Message createMessage(String uid) { + return new ImapMessage(uid, this); + } +} diff --git a/java/com/android/voicemail/impl/mail/store/ImapStore.java b/java/com/android/voicemail/impl/mail/store/ImapStore.java new file mode 100644 index 000000000..cadbe593f --- /dev/null +++ b/java/com/android/voicemail/impl/mail/store/ImapStore.java @@ -0,0 +1,181 @@ +/* + * 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.voicemail.impl.mail.store; + +import android.content.Context; +import android.net.Network; +import com.android.voicemail.impl.imap.ImapHelper; +import com.android.voicemail.impl.mail.MailTransport; +import com.android.voicemail.impl.mail.Message; +import com.android.voicemail.impl.mail.MessagingException; +import com.android.voicemail.impl.mail.internet.MimeMessage; +import java.io.IOException; +import java.io.InputStream; + +public class ImapStore { + /** + * A global suggestion to Store implementors on how much of the body should be returned on + * FetchProfile.Item.BODY_SANE requests. We'll use 125k now. + */ + public static final int FETCH_BODY_SANE_SUGGESTED_SIZE = (125 * 1024); + + private final Context mContext; + private final ImapHelper mHelper; + private final String mUsername; + private final String mPassword; + private final MailTransport mTransport; + private ImapConnection mConnection; + + public static final int FLAG_NONE = 0x00; // No flags + public static final int FLAG_SSL = 0x01; // Use SSL + public static final int FLAG_TLS = 0x02; // Use TLS + public static final int FLAG_AUTHENTICATE = 0x04; // Use name/password for authentication + public static final int FLAG_TRUST_ALL = 0x08; // Trust all certificates + public static final int FLAG_OAUTH = 0x10; // Use OAuth for authentication + + /** Contains all the information necessary to log into an imap server */ + public ImapStore( + Context context, + ImapHelper helper, + String username, + String password, + int port, + String serverName, + int flags, + Network network) { + mContext = context; + mHelper = helper; + mUsername = username; + mPassword = password; + mTransport = new MailTransport(context, this.getImapHelper(), network, serverName, port, flags); + } + + public Context getContext() { + return mContext; + } + + public ImapHelper getImapHelper() { + return mHelper; + } + + public String getUsername() { + return mUsername; + } + + public String getPassword() { + return mPassword; + } + + /** Returns a clone of the transport associated with this store. */ + MailTransport cloneTransport() { + return mTransport.clone(); + } + + /** Returns UIDs of Messages joined with "," as the separator. */ + static String joinMessageUids(Message[] messages) { + StringBuilder sb = new StringBuilder(); + boolean notFirst = false; + for (Message m : messages) { + if (notFirst) { + sb.append(','); + } + sb.append(m.getUid()); + notFirst = true; + } + return sb.toString(); + } + + static class ImapMessage extends MimeMessage { + private ImapFolder mFolder; + + ImapMessage(String uid, ImapFolder folder) { + mUid = uid; + mFolder = folder; + } + + public void setSize(int size) { + mSize = size; + } + + @Override + public void parse(InputStream in) throws IOException, MessagingException { + super.parse(in); + } + + public void setFlagInternal(String flag, boolean set) throws MessagingException { + super.setFlag(flag, set); + } + + @Override + public void setFlag(String flag, boolean set) throws MessagingException { + super.setFlag(flag, set); + mFolder.setFlags(new Message[] {this}, new String[] {flag}, set); + } + } + + static class ImapException extends MessagingException { + private static final long serialVersionUID = 1L; + + private final String mStatus; + private final String mStatusMessage; + private final String mAlertText; + private final String mResponseCode; + + public ImapException( + String message, + String status, + String statusMessage, + String alertText, + String responseCode) { + super(message); + mStatus = status; + mStatusMessage = statusMessage; + mAlertText = alertText; + mResponseCode = responseCode; + } + + public String getStatus() { + return mStatus; + } + + public String getStatusMessage() { + return mStatusMessage; + } + + public String getAlertText() { + return mAlertText; + } + + public String getResponseCode() { + return mResponseCode; + } + } + + public void closeConnection() { + if (mConnection != null) { + mConnection.close(); + mConnection = null; + } + } + + public ImapConnection getConnection() { + if (mConnection == null) { + mConnection = new ImapConnection(this); + } + return mConnection; + } +} diff --git a/java/com/android/voicemail/impl/mail/store/imap/DigestMd5Utils.java b/java/com/android/voicemail/impl/mail/store/imap/DigestMd5Utils.java new file mode 100644 index 000000000..f156f67c1 --- /dev/null +++ b/java/com/android/voicemail/impl/mail/store/imap/DigestMd5Utils.java @@ -0,0 +1,335 @@ +/* + * 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.voicemail.impl.mail.store.imap; + +import android.annotation.TargetApi; +import android.os.Build.VERSION_CODES; +import android.support.annotation.Nullable; +import android.support.annotation.VisibleForTesting; +import android.util.ArrayMap; +import android.util.Base64; +import com.android.voicemail.impl.VvmLog; +import com.android.voicemail.impl.mail.MailTransport; +import com.android.voicemail.impl.mail.MessagingException; +import com.android.voicemail.impl.mail.store.ImapStore; +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.security.SecureRandom; +import java.util.Map; + +@SuppressWarnings("AndroidApiChecker") // Map.getOrDefault() is java8 +@TargetApi(VERSION_CODES.O) +public class DigestMd5Utils { + + private static final String TAG = "DigestMd5Utils"; + + private static final String DIGEST_CHARSET = "CHARSET"; + private static final String DIGEST_USERNAME = "username"; + private static final String DIGEST_REALM = "realm"; + private static final String DIGEST_NONCE = "nonce"; + private static final String DIGEST_NC = "nc"; + private static final String DIGEST_CNONCE = "cnonce"; + private static final String DIGEST_URI = "digest-uri"; + private static final String DIGEST_RESPONSE = "response"; + private static final String DIGEST_QOP = "qop"; + + private static final String RESPONSE_AUTH_HEADER = "rspauth="; + private static final String HEX_CHARS = "0123456789abcdef"; + + /** Represents the set of data we need to generate the DIGEST-MD5 response. */ + public static class Data { + + private static final String CHARSET = "utf-8"; + + public String username; + public String password; + public String realm; + public String nonce; + public String nc; + public String cnonce; + public String digestUri; + public String qop; + + @VisibleForTesting + Data() { + // Do nothing + } + + public Data(ImapStore imapStore, MailTransport transport, Map challenge) { + username = imapStore.getUsername(); + password = imapStore.getPassword(); + realm = challenge.getOrDefault(DIGEST_REALM, ""); + nonce = challenge.get(DIGEST_NONCE); + cnonce = createCnonce(); + nc = "00000001"; // Subsequent Authentication not supported, nounce count always 1. + qop = "auth"; // Other config not supported + digestUri = "imap/" + transport.getHost(); + } + + private static String createCnonce() { + SecureRandom generator = new SecureRandom(); + + // At least 64 bits of entropy is required + byte[] rawBytes = new byte[8]; + generator.nextBytes(rawBytes); + + return Base64.encodeToString(rawBytes, Base64.NO_WRAP); + } + + /** Verify the response-auth returned by the server is correct. */ + public void verifyResponseAuth(String response) throws MessagingException { + if (!response.startsWith(RESPONSE_AUTH_HEADER)) { + throw new MessagingException("response-auth expected"); + } + if (!response + .substring(RESPONSE_AUTH_HEADER.length()) + .equals(DigestMd5Utils.getResponse(this, true))) { + throw new MessagingException("invalid response-auth return from the server."); + } + } + + public String createResponse() { + String response = getResponse(this, false); + ResponseBuilder builder = new ResponseBuilder(); + builder + .append(DIGEST_CHARSET, CHARSET) + .appendQuoted(DIGEST_USERNAME, username) + .appendQuoted(DIGEST_REALM, realm) + .appendQuoted(DIGEST_NONCE, nonce) + .append(DIGEST_NC, nc) + .appendQuoted(DIGEST_CNONCE, cnonce) + .appendQuoted(DIGEST_URI, digestUri) + .append(DIGEST_RESPONSE, response) + .append(DIGEST_QOP, qop); + return builder.toString(); + } + + private static class ResponseBuilder { + + private StringBuilder mBuilder = new StringBuilder(); + + public ResponseBuilder appendQuoted(String key, String value) { + if (mBuilder.length() != 0) { + mBuilder.append(","); + } + mBuilder.append(key).append("=\"").append(value).append("\""); + return this; + } + + public ResponseBuilder append(String key, String value) { + if (mBuilder.length() != 0) { + mBuilder.append(","); + } + mBuilder.append(key).append("=").append(value); + return this; + } + + @Override + public String toString() { + return mBuilder.toString(); + } + } + } + + /* + response-value = + toHex( getKeyDigest ( toHex(getMd5(a1)), + { nonce-value, ":" nc-value, ":", + cnonce-value, ":", qop-value, ":", toHex(getMd5(a2)) })) + * @param isResponseAuth is the response the one the server is returning us. response-auth has + * different a2 format. + */ + @VisibleForTesting + static String getResponse(Data data, boolean isResponseAuth) { + StringBuilder a1 = new StringBuilder(); + a1.append( + new String( + getMd5(data.username + ":" + data.realm + ":" + data.password), + StandardCharsets.ISO_8859_1)); + a1.append(":").append(data.nonce).append(":").append(data.cnonce); + + StringBuilder a2 = new StringBuilder(); + if (!isResponseAuth) { + a2.append("AUTHENTICATE"); + } + a2.append(":").append(data.digestUri); + + return toHex( + getKeyDigest( + toHex(getMd5(a1.toString())), + data.nonce + + ":" + + data.nc + + ":" + + data.cnonce + + ":" + + data.qop + + ":" + + toHex(getMd5(a2.toString())))); + } + + /** Let getMd5(s) be the 16 octet MD5 hash [RFC 1321] of the octet string s. */ + private static byte[] getMd5(String s) { + try { + MessageDigest digester = MessageDigest.getInstance("MD5"); + digester.update(s.getBytes(StandardCharsets.ISO_8859_1)); + return digester.digest(); + } catch (NoSuchAlgorithmException e) { + throw new AssertionError(e); + } + } + + /** + * Let getKeyDigest(k, s) be getMd5({k, ":", s}), i.e., the 16 octet hash of the string k, a colon + * and the string s. + */ + private static byte[] getKeyDigest(String k, String s) { + StringBuilder builder = new StringBuilder(k).append(":").append(s); + return getMd5(builder.toString()); + } + + /** + * Let toHex(n) be the representation of the 16 octet MD5 hash n as a string of 32 hex digits + * (with alphabetic characters always in lower case, since MD5 is case sensitive). + */ + private static String toHex(byte[] n) { + StringBuilder result = new StringBuilder(); + for (byte b : n) { + int unsignedByte = b & 0xFF; + result + .append(HEX_CHARS.charAt(unsignedByte / 16)) + .append(HEX_CHARS.charAt(unsignedByte % 16)); + } + return result.toString(); + } + + public static Map parseDigestMessage(String message) throws MessagingException { + Map result = new DigestMessageParser(message).parse(); + if (!result.containsKey(DIGEST_NONCE)) { + throw new MessagingException("nonce missing from server DIGEST-MD5 challenge"); + } + return result; + } + + /** Parse the key-value pair returned by the server. */ + private static class DigestMessageParser { + + private final String mMessage; + private int mPosition = 0; + private Map mResult = new ArrayMap<>(); + + public DigestMessageParser(String message) { + mMessage = message; + } + + @Nullable + public Map parse() { + try { + while (mPosition < mMessage.length()) { + parsePair(); + if (mPosition != mMessage.length()) { + expect(','); + } + } + } catch (IndexOutOfBoundsException e) { + VvmLog.e(TAG, e.toString()); + return null; + } + return mResult; + } + + private void parsePair() { + String key = parseKey(); + expect('='); + String value = parseValue(); + mResult.put(key, value); + } + + private void expect(char c) { + if (pop() != c) { + throw new IllegalStateException("unexpected character " + mMessage.charAt(mPosition)); + } + } + + private char pop() { + char result = peek(); + mPosition++; + return result; + } + + private char peek() { + return mMessage.charAt(mPosition); + } + + private void goToNext(char c) { + while (peek() != c) { + mPosition++; + } + } + + private String parseKey() { + int start = mPosition; + goToNext('='); + return mMessage.substring(start, mPosition); + } + + private String parseValue() { + if (peek() == '"') { + return parseQuotedValue(); + } else { + return parseUnquotedValue(); + } + } + + private String parseQuotedValue() { + expect('"'); + StringBuilder result = new StringBuilder(); + while (true) { + char c = pop(); + if (c == '\\') { + result.append(pop()); + } else if (c == '"') { + break; + } else { + result.append(c); + } + } + return result.toString(); + } + + private String parseUnquotedValue() { + StringBuilder result = new StringBuilder(); + while (true) { + char c = pop(); + if (c == '\\') { + result.append(pop()); + } else if (c == ',') { + mPosition--; + break; + } else { + result.append(c); + } + + if (mPosition == mMessage.length()) { + break; + } + } + return result.toString(); + } + } +} diff --git a/java/com/android/voicemail/impl/mail/store/imap/ImapConstants.java b/java/com/android/voicemail/impl/mail/store/imap/ImapConstants.java new file mode 100644 index 000000000..88ec0ed90 --- /dev/null +++ b/java/com/android/voicemail/impl/mail/store/imap/ImapConstants.java @@ -0,0 +1,138 @@ +/* + * 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.voicemail.impl.mail.store.imap; + +import com.android.voicemail.impl.mail.store.ImapStore; +import java.util.Locale; + +public final class ImapConstants { + private ImapConstants() {} + + public static final String FETCH_FIELD_BODY_PEEK_BARE = "BODY.PEEK"; + public static final String FETCH_FIELD_BODY_PEEK = FETCH_FIELD_BODY_PEEK_BARE + "[]"; + public static final String FETCH_FIELD_BODY_PEEK_SANE = + String.format(Locale.US, "BODY.PEEK[]<0.%d>", ImapStore.FETCH_BODY_SANE_SUGGESTED_SIZE); + public static final String FETCH_FIELD_HEADERS = + "BODY.PEEK[HEADER.FIELDS (date subject from content-type to cc message-id)]"; + + public static final String ALERT = "ALERT"; + public static final String APPEND = "APPEND"; + public static final String AUTHENTICATE = "AUTHENTICATE"; + public static final String BAD = "BAD"; + public static final String BADCHARSET = "BADCHARSET"; + public static final String BODY = "BODY"; + public static final String BODY_BRACKET_HEADER = "BODY[HEADER"; + public static final String BODYSTRUCTURE = "BODYSTRUCTURE"; + public static final String BYE = "BYE"; + public static final String CAPABILITY = "CAPABILITY"; + public static final String CHECK = "CHECK"; + public static final String CLOSE = "CLOSE"; + public static final String COPY = "COPY"; + public static final String COPYUID = "COPYUID"; + public static final String CREATE = "CREATE"; + public static final String DELETE = "DELETE"; + public static final String EXAMINE = "EXAMINE"; + public static final String EXISTS = "EXISTS"; + public static final String EXPUNGE = "EXPUNGE"; + public static final String FETCH = "FETCH"; + public static final String FLAG_ANSWERED = "\\ANSWERED"; + public static final String FLAG_DELETED = "\\DELETED"; + public static final String FLAG_FLAGGED = "\\FLAGGED"; + public static final String FLAG_NO_SELECT = "\\NOSELECT"; + public static final String FLAG_SEEN = "\\SEEN"; + public static final String FLAGS = "FLAGS"; + public static final String FLAGS_SILENT = "FLAGS.SILENT"; + public static final String ID = "ID"; + public static final String INBOX = "INBOX"; + public static final String INTERNALDATE = "INTERNALDATE"; + public static final String LIST = "LIST"; + public static final String LOGIN = "LOGIN"; + public static final String LOGOUT = "LOGOUT"; + public static final String LSUB = "LSUB"; + public static final String NAMESPACE = "NAMESPACE"; + public static final String NO = "NO"; + public static final String NOOP = "NOOP"; + public static final String OK = "OK"; + public static final String PARSE = "PARSE"; + public static final String PERMANENTFLAGS = "PERMANENTFLAGS"; + public static final String PREAUTH = "PREAUTH"; + public static final String READ_ONLY = "READ-ONLY"; + public static final String READ_WRITE = "READ-WRITE"; + public static final String RENAME = "RENAME"; + public static final String RFC822_SIZE = "RFC822.SIZE"; + public static final String SEARCH = "SEARCH"; + public static final String SELECT = "SELECT"; + public static final String STARTTLS = "STARTTLS"; + public static final String STATUS = "STATUS"; + public static final String STORE = "STORE"; + public static final String SUBSCRIBE = "SUBSCRIBE"; + public static final String TEXT = "TEXT"; + public static final String TRYCREATE = "TRYCREATE"; + public static final String UID = "UID"; + public static final String UID_COPY = "UID COPY"; + public static final String UID_FETCH = "UID FETCH"; + public static final String UID_SEARCH = "UID SEARCH"; + public static final String UID_STORE = "UID STORE"; + public static final String UIDNEXT = "UIDNEXT"; + public static final String UIDPLUS = "UIDPLUS"; + public static final String UIDVALIDITY = "UIDVALIDITY"; + public static final String UNSEEN = "UNSEEN"; + public static final String UNSUBSCRIBE = "UNSUBSCRIBE"; + public static final String XOAUTH2 = "XOAUTH2"; + public static final String APPENDUID = "APPENDUID"; + public static final String NIL = "NIL"; + + /** NO responses */ + public static final String NO_COMMAND_NOT_ALLOWED = "command not allowed"; + + public static final String NO_RESERVATION_FAILED = "reservation failed"; + public static final String NO_APPLICATION_ERROR = "application error"; + public static final String NO_INVALID_PARAMETER = "invalid parameter"; + public static final String NO_INVALID_COMMAND = "invalid command"; + public static final String NO_UNKNOWN_COMMAND = "unknown command"; + // AUTHENTICATE + // The subscriber can not be located in the system. + public static final String NO_UNKNOWN_USER = "unknown user"; + // The Client Type or Protocol Version is unknown. + public static final String NO_UNKNOWN_CLIENT = "unknown client"; + // The password received from the client does not match the password defined in the subscriber's + // profile. + public static final String NO_INVALID_PASSWORD = "invalid password"; + // The subscriber's mailbox has not yet been initialised via the TUI + public static final String NO_MAILBOX_NOT_INITIALIZED = "mailbox not initialized"; + // The subscriber has not been provisioned for the VVM service. + public static final String NO_SERVICE_IS_NOT_PROVISIONED = "service is not provisioned"; + // The subscriber is provisioned for the VVM service but the VVM service is currently not active + public static final String NO_SERVICE_IS_NOT_ACTIVATED = "service is not activated"; + // The Voice Mail Blocked flag in the subscriber's profile is set to YES. + public static final String NO_USER_IS_BLOCKED = "user is blocked"; + + /** extensions */ + public static final String GETQUOTA = "GETQUOTA"; + + public static final String GETQUOTAROOT = "GETQUOTAROOT"; + public static final String QUOTAROOT = "QUOTAROOT"; + public static final String QUOTA = "QUOTA"; + + /** capabilities */ + public static final String CAPABILITY_AUTH_DIGEST_MD5 = "AUTH=DIGEST-MD5"; + + public static final String CAPABILITY_STARTTLS = "STARTTLS"; + + /** authentication */ + public static final String AUTH_DIGEST_MD5 = "DIGEST-MD5"; +} diff --git a/java/com/android/voicemail/impl/mail/store/imap/ImapElement.java b/java/com/android/voicemail/impl/mail/store/imap/ImapElement.java new file mode 100644 index 000000000..ee255d1eb --- /dev/null +++ b/java/com/android/voicemail/impl/mail/store/imap/ImapElement.java @@ -0,0 +1,124 @@ +/* + * 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.voicemail.impl.mail.store.imap; + +/** + * Class representing "element"s in IMAP responses. + * + *

Class hierarchy: + * + *

+ * ImapElement
+ *   |
+ *   |-- ImapElement.NONE (for 'index out of range')
+ *   |
+ *   |-- ImapList (isList() == true)
+ *   |   |
+ *   |   |-- ImapList.EMPTY
+ *   |   |
+ *   |   --- ImapResponse
+ *   |
+ *   --- ImapString (isString() == true)
+ *       |
+ *       |-- ImapString.EMPTY
+ *       |
+ *       |-- ImapSimpleString
+ *       |
+ *       |-- ImapMemoryLiteral
+ *       |
+ *       --- ImapTempFileLiteral
+ * 
+ */ +public abstract class ImapElement { + /** + * An element that is returned by {@link ImapList#getElementOrNone} to indicate an index is out of + * range. + */ + public static final ImapElement NONE = + new ImapElement() { + @Override + public void destroy() { + // Don't call super.destroy(). + // It's a shared object. We don't want the mDestroyed to be set on this. + } + + @Override + public boolean isList() { + return false; + } + + @Override + public boolean isString() { + return false; + } + + @Override + public String toString() { + return "[NO ELEMENT]"; + } + + @Override + public boolean equalsForTest(ImapElement that) { + return super.equalsForTest(that); + } + }; + + private boolean mDestroyed = false; + + public abstract boolean isList(); + + public abstract boolean isString(); + + protected boolean isDestroyed() { + return mDestroyed; + } + + /** + * Clean up the resources used by the instance. It's for removing a temp file used by {@link + * ImapTempFileLiteral}. + */ + public void destroy() { + mDestroyed = true; + } + + /** Throws {@link RuntimeException} if it's already destroyed. */ + protected final void checkNotDestroyed() { + if (mDestroyed) { + throw new RuntimeException("Already destroyed"); + } + } + + /** + * Return a string that represents this object; it's purely for the debug purpose. Don't mistake + * it for {@link ImapString#getString}. + * + *

Abstract to force subclasses to implement it. + */ + @Override + public abstract String toString(); + + /** + * The equals implementation that is intended to be used only for unit testing. (Because it may be + * heavy and has a special sense of "equal" for testing.) + */ + public boolean equalsForTest(ImapElement that) { + if (that == null) { + return false; + } + return this.getClass() == that.getClass(); // Has to be the same class. + } +} diff --git a/java/com/android/voicemail/impl/mail/store/imap/ImapList.java b/java/com/android/voicemail/impl/mail/store/imap/ImapList.java new file mode 100644 index 000000000..e4a6ec0ac --- /dev/null +++ b/java/com/android/voicemail/impl/mail/store/imap/ImapList.java @@ -0,0 +1,226 @@ +/* + * 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.voicemail.impl.mail.store.imap; + +import java.util.ArrayList; + +/** Class represents an IMAP list. */ +public class ImapList extends ImapElement { + /** {@link ImapList} representing an empty list. */ + public static final ImapList EMPTY = + new ImapList() { + @Override + public void destroy() { + // Don't call super.destroy(). + // It's a shared object. We don't want the mDestroyed to be set on this. + } + + @Override + void add(ImapElement e) { + throw new RuntimeException(); + } + }; + + private ArrayList mList = new ArrayList(); + + /* package */ void add(ImapElement e) { + if (e == null) { + throw new RuntimeException("Can't add null"); + } + mList.add(e); + } + + @Override + public final boolean isString() { + return false; + } + + @Override + public final boolean isList() { + return true; + } + + public final int size() { + return mList.size(); + } + + public final boolean isEmpty() { + return size() == 0; + } + + /** + * Return true if the element at {@code index} exists, is string, and equals to {@code s}. (case + * insensitive) + */ + public final boolean is(int index, String s) { + return is(index, s, false); + } + + /** Same as {@link #is(int, String)}, but does the prefix match if {@code prefixMatch}. */ + public final boolean is(int index, String s, boolean prefixMatch) { + if (!prefixMatch) { + return getStringOrEmpty(index).is(s); + } else { + return getStringOrEmpty(index).startsWith(s); + } + } + + /** + * Return the element at {@code index}. If {@code index} is out of range, returns {@link + * ImapElement#NONE}. + */ + public final ImapElement getElementOrNone(int index) { + return (index >= mList.size()) ? ImapElement.NONE : mList.get(index); + } + + /** + * Return the element at {@code index} if it's a list. If {@code index} is out of range or not a + * list, returns {@link ImapList#EMPTY}. + */ + public final ImapList getListOrEmpty(int index) { + ImapElement el = getElementOrNone(index); + return el.isList() ? (ImapList) el : EMPTY; + } + + /** + * Return the element at {@code index} if it's a string. If {@code index} is out of range or not a + * string, returns {@link ImapString#EMPTY}. + */ + public final ImapString getStringOrEmpty(int index) { + ImapElement el = getElementOrNone(index); + return el.isString() ? (ImapString) el : ImapString.EMPTY; + } + + /** + * Return an element keyed by {@code key}. Return null if not found. {@code key} has to be at an + * even index. + */ + /* package */ final ImapElement getKeyedElementOrNull(String key, boolean prefixMatch) { + for (int i = 1; i < size(); i += 2) { + if (is(i - 1, key, prefixMatch)) { + return mList.get(i); + } + } + return null; + } + + /** + * Return an {@link ImapList} keyed by {@code key}. Return {@link ImapList#EMPTY} if not found. + */ + public final ImapList getKeyedListOrEmpty(String key) { + return getKeyedListOrEmpty(key, false); + } + + /** + * Return an {@link ImapList} keyed by {@code key}. Return {@link ImapList#EMPTY} if not found. + */ + public final ImapList getKeyedListOrEmpty(String key, boolean prefixMatch) { + ImapElement e = getKeyedElementOrNull(key, prefixMatch); + return (e != null) ? ((ImapList) e) : ImapList.EMPTY; + } + + /** + * Return an {@link ImapString} keyed by {@code key}. Return {@link ImapString#EMPTY} if not + * found. + */ + public final ImapString getKeyedStringOrEmpty(String key) { + return getKeyedStringOrEmpty(key, false); + } + + /** + * Return an {@link ImapString} keyed by {@code key}. Return {@link ImapString#EMPTY} if not + * found. + */ + public final ImapString getKeyedStringOrEmpty(String key, boolean prefixMatch) { + ImapElement e = getKeyedElementOrNull(key, prefixMatch); + return (e != null) ? ((ImapString) e) : ImapString.EMPTY; + } + + /** Return true if it contains {@code s}. */ + public final boolean contains(String s) { + for (int i = 0; i < size(); i++) { + if (getStringOrEmpty(i).is(s)) { + return true; + } + } + return false; + } + + @Override + public void destroy() { + if (mList != null) { + for (ImapElement e : mList) { + e.destroy(); + } + mList = null; + } + super.destroy(); + } + + @Override + public String toString() { + return mList.toString(); + } + + /** Return the text representations of the contents concatenated with ",". */ + public final String flatten() { + return flatten(new StringBuilder()).toString(); + } + + /** + * Returns text representations (i.e. getString()) of contents joined together with "," as the + * separator. + * + *

Only used for building the capability string passed to vendor policies. + * + *

We can't use toString(), because it's for debugging (meaning the format may change any + * time), and it won't expand literals. + */ + private final StringBuilder flatten(StringBuilder sb) { + sb.append('['); + for (int i = 0; i < mList.size(); i++) { + if (i > 0) { + sb.append(','); + } + final ImapElement e = getElementOrNone(i); + if (e.isList()) { + getListOrEmpty(i).flatten(sb); + } else if (e.isString()) { + sb.append(getStringOrEmpty(i).getString()); + } + } + sb.append(']'); + return sb; + } + + @Override + public boolean equalsForTest(ImapElement that) { + if (!super.equalsForTest(that)) { + return false; + } + ImapList thatList = (ImapList) that; + if (size() != thatList.size()) { + return false; + } + for (int i = 0; i < size(); i++) { + if (!mList.get(i).equalsForTest(thatList.getElementOrNone(i))) { + return false; + } + } + return true; + } +} diff --git a/java/com/android/voicemail/impl/mail/store/imap/ImapMemoryLiteral.java b/java/com/android/voicemail/impl/mail/store/imap/ImapMemoryLiteral.java new file mode 100644 index 000000000..96a8c4ae5 --- /dev/null +++ b/java/com/android/voicemail/impl/mail/store/imap/ImapMemoryLiteral.java @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2010 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.voicemail.impl.mail.store.imap; + +import com.android.voicemail.impl.VvmLog; +import com.android.voicemail.impl.mail.FixedLengthInputStream; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.UnsupportedEncodingException; + +/** Subclass of {@link ImapString} used for literals backed by an in-memory byte array. */ +public class ImapMemoryLiteral extends ImapString { + private final String TAG = "ImapMemoryLiteral"; + private byte[] mData; + + /* package */ ImapMemoryLiteral(FixedLengthInputStream in) throws IOException { + // We could use ByteArrayOutputStream and IOUtils.copy, but it'd perform an unnecessary + // copy.... + mData = new byte[in.getLength()]; + int pos = 0; + while (pos < mData.length) { + int read = in.read(mData, pos, mData.length - pos); + if (read < 0) { + break; + } + pos += read; + } + if (pos != mData.length) { + VvmLog.w(TAG, "length mismatch"); + } + } + + @Override + public void destroy() { + mData = null; + super.destroy(); + } + + @Override + public String getString() { + try { + return new String(mData, "US-ASCII"); + } catch (UnsupportedEncodingException e) { + VvmLog.e(TAG, "Unsupported encoding: ", e); + } + return null; + } + + @Override + public InputStream getAsStream() { + return new ByteArrayInputStream(mData); + } + + @Override + public String toString() { + return String.format("{%d byte literal(memory)}", mData.length); + } +} diff --git a/java/com/android/voicemail/impl/mail/store/imap/ImapResponse.java b/java/com/android/voicemail/impl/mail/store/imap/ImapResponse.java new file mode 100644 index 000000000..d53d458da --- /dev/null +++ b/java/com/android/voicemail/impl/mail/store/imap/ImapResponse.java @@ -0,0 +1,142 @@ +/* + * 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.voicemail.impl.mail.store.imap; + +/** Class represents an IMAP response. */ +public class ImapResponse extends ImapList { + private final String mTag; + private final boolean mIsContinuationRequest; + + /* package */ ImapResponse(String tag, boolean isContinuationRequest) { + mTag = tag; + mIsContinuationRequest = isContinuationRequest; + } + + /* package */ static boolean isStatusResponse(String symbol) { + return ImapConstants.OK.equalsIgnoreCase(symbol) + || ImapConstants.NO.equalsIgnoreCase(symbol) + || ImapConstants.BAD.equalsIgnoreCase(symbol) + || ImapConstants.PREAUTH.equalsIgnoreCase(symbol) + || ImapConstants.BYE.equalsIgnoreCase(symbol); + } + + /** @return whether it's a tagged response. */ + public boolean isTagged() { + return mTag != null; + } + + /** @return whether it's a continuation request. */ + public boolean isContinuationRequest() { + return mIsContinuationRequest; + } + + public boolean isStatusResponse() { + return isStatusResponse(getStringOrEmpty(0).getString()); + } + + /** @return whether it's an OK response. */ + public boolean isOk() { + return is(0, ImapConstants.OK); + } + + /** @return whether it's an BAD response. */ + public boolean isBad() { + return is(0, ImapConstants.BAD); + } + + /** @return whether it's an NO response. */ + public boolean isNo() { + return is(0, ImapConstants.NO); + } + + /** + * @return whether it's an {@code responseType} data response. (i.e. not tagged). + * @param index where {@code responseType} should appear. e.g. 1 for "FETCH" + * @param responseType e.g. "FETCH" + */ + public final boolean isDataResponse(int index, String responseType) { + return !isTagged() && getStringOrEmpty(index).is(responseType); + } + + /** + * @return Response code (RFC 3501 7.1) if it's a status response. + *

e.g. "ALERT" for "* OK [ALERT] System shutdown in 10 minutes" + */ + public ImapString getResponseCodeOrEmpty() { + if (!isStatusResponse()) { + return ImapString.EMPTY; // Not a status response. + } + return getListOrEmpty(1).getStringOrEmpty(0); + } + + /** + * @return Alert message it it has ALERT response code. + *

e.g. "System shutdown in 10 minutes" for "* OK [ALERT] System shutdown in 10 minutes" + */ + public ImapString getAlertTextOrEmpty() { + if (!getResponseCodeOrEmpty().is(ImapConstants.ALERT)) { + return ImapString.EMPTY; // Not an ALERT + } + // The 3rd element contains all the rest of line. + return getStringOrEmpty(2); + } + + /** @return Response text in a status response. */ + public ImapString getStatusResponseTextOrEmpty() { + if (!isStatusResponse()) { + return ImapString.EMPTY; + } + return getStringOrEmpty(getElementOrNone(1).isList() ? 2 : 1); + } + + public ImapString getStatusOrEmpty() { + if (!isStatusResponse()) { + return ImapString.EMPTY; + } + return getStringOrEmpty(0); + } + + @Override + public String toString() { + String tag = mTag; + if (isContinuationRequest()) { + tag = "+"; + } + return "#" + tag + "# " + super.toString(); + } + + @Override + public boolean equalsForTest(ImapElement that) { + if (!super.equalsForTest(that)) { + return false; + } + final ImapResponse thatResponse = (ImapResponse) that; + if (mTag == null) { + if (thatResponse.mTag != null) { + return false; + } + } else { + if (!mTag.equals(thatResponse.mTag)) { + return false; + } + } + if (mIsContinuationRequest != thatResponse.mIsContinuationRequest) { + return false; + } + return true; + } +} diff --git a/java/com/android/voicemail/impl/mail/store/imap/ImapResponseParser.java b/java/com/android/voicemail/impl/mail/store/imap/ImapResponseParser.java new file mode 100644 index 000000000..e37106a69 --- /dev/null +++ b/java/com/android/voicemail/impl/mail/store/imap/ImapResponseParser.java @@ -0,0 +1,424 @@ +/* + * Copyright (C) 2010 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.voicemail.impl.mail.store.imap; + +import android.text.TextUtils; +import com.android.voicemail.impl.VvmLog; +import com.android.voicemail.impl.mail.FixedLengthInputStream; +import com.android.voicemail.impl.mail.MessagingException; +import com.android.voicemail.impl.mail.PeekableInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; + +/** IMAP response parser. */ +public class ImapResponseParser { + private static final String TAG = "ImapResponseParser"; + + /** Literal larger than this will be stored in temp file. */ + public static final int LITERAL_KEEP_IN_MEMORY_THRESHOLD = 2 * 1024 * 1024; + + /** Input stream */ + private final PeekableInputStream mIn; + + private final int mLiteralKeepInMemoryThreshold; + + /** StringBuilder used by readUntil() */ + private final StringBuilder mBufferReadUntil = new StringBuilder(); + + /** StringBuilder used by parseBareString() */ + private final StringBuilder mParseBareString = new StringBuilder(); + + /** + * We store all {@link ImapResponse} in it. {@link #destroyResponses()} must be called from time + * to time to destroy them and clear it. + */ + private final ArrayList mResponsesToDestroy = new ArrayList(); + + /** + * Exception thrown when we receive BYE. It derives from IOException, so it'll be treated in the + * same way EOF does. + */ + public static class ByeException extends IOException { + public static final String MESSAGE = "Received BYE"; + + public ByeException() { + super(MESSAGE); + } + } + + /** Public constructor for normal use. */ + public ImapResponseParser(InputStream in) { + this(in, LITERAL_KEEP_IN_MEMORY_THRESHOLD); + } + + /** Constructor for testing to override the literal size threshold. */ + /* package for test */ ImapResponseParser(InputStream in, int literalKeepInMemoryThreshold) { + mIn = new PeekableInputStream(in); + mLiteralKeepInMemoryThreshold = literalKeepInMemoryThreshold; + } + + private static IOException newEOSException() { + final String message = "End of stream reached"; + VvmLog.d(TAG, message); + return new IOException(message); + } + + /** + * Peek next one byte. + * + *

Throws IOException() if reaches EOF. As long as logical response lines end with \r\n, we + * shouldn't see EOF during parsing. + */ + private int peek() throws IOException { + final int next = mIn.peek(); + if (next == -1) { + throw newEOSException(); + } + return next; + } + + /** + * Read and return one byte from {@link #mIn}, and put it in {@link #mDiscourseLogger}. + * + *

Throws IOException() if reaches EOF. As long as logical response lines end with \r\n, we + * shouldn't see EOF during parsing. + */ + private int readByte() throws IOException { + int next = mIn.read(); + if (next == -1) { + throw newEOSException(); + } + return next; + } + + /** + * Destroy all the {@link ImapResponse}s stored in the internal storage and clear it. + * + * @see #readResponse() + */ + public void destroyResponses() { + for (ImapResponse r : mResponsesToDestroy) { + r.destroy(); + } + mResponsesToDestroy.clear(); + } + + /** + * Reads the next response available on the stream and returns an {@link ImapResponse} object that + * represents it. + * + *

When this method successfully returns an {@link ImapResponse}, the {@link ImapResponse} is + * stored in the internal storage. When the {@link ImapResponse} is no longer used {@link + * #destroyResponses} should be called to destroy all the responses in the array. + * + * @param byeExpected is a untagged BYE response expected? If not proper cleanup will be done and + * {@link ByeException} will be thrown. + * @return the parsed {@link ImapResponse} object. + * @exception ByeException when detects BYE and byeExpected is false. + */ + public ImapResponse readResponse(boolean byeExpected) throws IOException, MessagingException { + ImapResponse response = null; + try { + response = parseResponse(); + } catch (RuntimeException e) { + // Parser crash -- log network activities. + onParseError(e); + throw e; + } catch (IOException e) { + // Network error, or received an unexpected char. + onParseError(e); + throw e; + } + + // Handle this outside of try-catch. We don't have to dump protocol log when getting BYE. + if (!byeExpected && response.is(0, ImapConstants.BYE)) { + VvmLog.w(TAG, ByeException.MESSAGE); + response.destroy(); + throw new ByeException(); + } + mResponsesToDestroy.add(response); + return response; + } + + private void onParseError(Exception e) { + // Read a few more bytes, so that the log will contain some more context, even if the parser + // crashes in the middle of a response. + // This also makes sure the byte in question will be logged, no matter where it crashes. + // e.g. when parseAtom() peeks and finds at an unexpected char, it throws an exception + // before actually reading it. + // However, we don't want to read too much, because then it may get into an email message. + try { + for (int i = 0; i < 4; i++) { + int b = readByte(); + if (b == -1 || b == '\n') { + break; + } + } + } catch (IOException ignore) { + } + VvmLog.w(TAG, "Exception detected: " + e.getMessage()); + } + + /** + * Read next byte from stream and throw it away. If the byte is different from {@code expected} + * throw {@link MessagingException}. + */ + /* package for test */ void expect(char expected) throws IOException { + final int next = readByte(); + if (expected != next) { + throw new IOException( + String.format( + "Expected %04x (%c) but got %04x (%c)", (int) expected, expected, next, (char) next)); + } + } + + /** + * Read bytes until we find {@code end}, and return all as string. The {@code end} will be read + * (rather than peeked) and won't be included in the result. + */ + /* package for test */ String readUntil(char end) throws IOException { + mBufferReadUntil.setLength(0); + for (; ; ) { + final int ch = readByte(); + if (ch != end) { + mBufferReadUntil.append((char) ch); + } else { + return mBufferReadUntil.toString(); + } + } + } + + /** Read all bytes until \r\n. */ + /* package */ String readUntilEol() throws IOException { + String ret = readUntil('\r'); + expect('\n'); // TODO Should this really be error? + return ret; + } + + /** Parse and return the response line. */ + private ImapResponse parseResponse() throws IOException, MessagingException { + // We need to destroy the response if we get an exception. + // So, we first store the response that's being built in responseToDestroy, until it's + // completely built, at which point we copy it into responseToReturn and null out + // responseToDestroyt. + // If responseToDestroy is not null in finally, we destroy it because that means + // we got an exception somewhere. + ImapResponse responseToDestroy = null; + final ImapResponse responseToReturn; + + try { + final int ch = peek(); + if (ch == '+') { // Continuation request + readByte(); // skip + + expect(' '); + responseToDestroy = new ImapResponse(null, true); + + // If it's continuation request, we don't really care what's in it. + responseToDestroy.add(new ImapSimpleString(readUntilEol())); + + // Response has successfully been built. Let's return it. + responseToReturn = responseToDestroy; + responseToDestroy = null; + } else { + // Status response or response data + final String tag; + if (ch == '*') { + tag = null; + readByte(); // skip * + expect(' '); + } else { + tag = readUntil(' '); + } + responseToDestroy = new ImapResponse(tag, false); + + final ImapString firstString = parseBareString(); + responseToDestroy.add(firstString); + + // parseBareString won't eat a space after the string, so we need to skip it, + // if exists. + // If the next char is not ' ', it should be EOL. + if (peek() == ' ') { + readByte(); // skip ' ' + + if (responseToDestroy.isStatusResponse()) { // It's a status response + + // Is there a response code? + final int next = peek(); + if (next == '[') { + responseToDestroy.add(parseList('[', ']')); + if (peek() == ' ') { // Skip following space + readByte(); + } + } + + String rest = readUntilEol(); + if (!TextUtils.isEmpty(rest)) { + // The rest is free-form text. + responseToDestroy.add(new ImapSimpleString(rest)); + } + } else { // It's a response data. + parseElements(responseToDestroy, '\0'); + } + } else { + expect('\r'); + expect('\n'); + } + + // Response has successfully been built. Let's return it. + responseToReturn = responseToDestroy; + responseToDestroy = null; + } + } finally { + if (responseToDestroy != null) { + // We get an exception. + responseToDestroy.destroy(); + } + } + + return responseToReturn; + } + + private ImapElement parseElement() throws IOException, MessagingException { + final int next = peek(); + switch (next) { + case '(': + return parseList('(', ')'); + case '[': + return parseList('[', ']'); + case '"': + readByte(); // Skip " + return new ImapSimpleString(readUntil('"')); + case '{': + return parseLiteral(); + case '\r': // CR + readByte(); // Consume \r + expect('\n'); // Should be followed by LF. + return null; + case '\n': // LF // There shouldn't be a bare LF, but just in case. + readByte(); // Consume \n + return null; + default: + return parseBareString(); + } + } + + /** + * Parses an atom. + * + *

Special case: If an atom contains '[', everything until the next ']' will be considered a + * part of the atom. (e.g. "BODY[HEADER.FIELDS ("DATE" ...)]" will become a single ImapString) + * + *

If the value is "NIL", returns an empty string. + */ + private ImapString parseBareString() throws IOException, MessagingException { + mParseBareString.setLength(0); + for (; ; ) { + final int ch = peek(); + + // TODO Can we clean this up? (This condition is from the old parser.) + if (ch == '(' + || ch == ')' + || ch == '{' + || ch == ' ' + || + // ']' is not part of atom (it's in resp-specials) + ch == ']' + || + // docs claim that flags are \ atom but atom isn't supposed to + // contain + // * and some flags contain * + // ch == '%' || ch == '*' || + ch == '%' + || + // TODO probably should not allow \ and should recognize + // it as a flag instead + // ch == '"' || ch == '\' || + ch == '"' + || (0x00 <= ch && ch <= 0x1f) + || ch == 0x7f) { + if (mParseBareString.length() == 0) { + throw new MessagingException("Expected string, none found."); + } + String s = mParseBareString.toString(); + + // NIL will be always converted into the empty string. + if (ImapConstants.NIL.equalsIgnoreCase(s)) { + return ImapString.EMPTY; + } + return new ImapSimpleString(s); + } else if (ch == '[') { + // Eat all until next ']' + mParseBareString.append((char) readByte()); + mParseBareString.append(readUntil(']')); + mParseBareString.append(']'); // readUntil won't include the end char. + } else { + mParseBareString.append((char) readByte()); + } + } + } + + private void parseElements(ImapList list, char end) throws IOException, MessagingException { + for (; ; ) { + for (; ; ) { + final int next = peek(); + if (next == end) { + return; + } + if (next != ' ') { + break; + } + // Skip space + readByte(); + } + final ImapElement el = parseElement(); + if (el == null) { // EOL + return; + } + list.add(el); + } + } + + private ImapList parseList(char opening, char closing) throws IOException, MessagingException { + expect(opening); + final ImapList list = new ImapList(); + parseElements(list, closing); + expect(closing); + return list; + } + + private ImapString parseLiteral() throws IOException, MessagingException { + expect('{'); + final int size; + try { + size = Integer.parseInt(readUntil('}')); + } catch (NumberFormatException nfe) { + throw new MessagingException("Invalid length in literal"); + } + if (size < 0) { + throw new MessagingException("Invalid negative length in literal"); + } + expect('\r'); + expect('\n'); + FixedLengthInputStream in = new FixedLengthInputStream(mIn, size); + if (size > mLiteralKeepInMemoryThreshold) { + return new ImapTempFileLiteral(in); + } else { + return new ImapMemoryLiteral(in); + } + } +} diff --git a/java/com/android/voicemail/impl/mail/store/imap/ImapSimpleString.java b/java/com/android/voicemail/impl/mail/store/imap/ImapSimpleString.java new file mode 100644 index 000000000..7cc866b74 --- /dev/null +++ b/java/com/android/voicemail/impl/mail/store/imap/ImapSimpleString.java @@ -0,0 +1,59 @@ +/* + * 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.voicemail.impl.mail.store.imap; + +import com.android.voicemail.impl.VvmLog; +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.io.UnsupportedEncodingException; + +/** Subclass of {@link ImapString} used for non literals. */ +public class ImapSimpleString extends ImapString { + private final String TAG = "ImapSimpleString"; + private String mString; + + /* package */ ImapSimpleString(String string) { + mString = (string != null) ? string : ""; + } + + @Override + public void destroy() { + mString = null; + super.destroy(); + } + + @Override + public String getString() { + return mString; + } + + @Override + public InputStream getAsStream() { + try { + return new ByteArrayInputStream(mString.getBytes("US-ASCII")); + } catch (UnsupportedEncodingException e) { + VvmLog.e(TAG, "Unsupported encoding: ", e); + } + return null; + } + + @Override + public String toString() { + // Purposefully not return just mString, in order to prevent using it instead of getString. + return "\"" + mString + "\""; + } +} diff --git a/java/com/android/voicemail/impl/mail/store/imap/ImapString.java b/java/com/android/voicemail/impl/mail/store/imap/ImapString.java new file mode 100644 index 000000000..d5c555126 --- /dev/null +++ b/java/com/android/voicemail/impl/mail/store/imap/ImapString.java @@ -0,0 +1,179 @@ +/* + * 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.voicemail.impl.mail.store.imap; + +import com.android.voicemail.impl.VvmLog; +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Locale; + +/** + * Class represents an IMAP "element" that is not a list. + * + *

An atom, quoted string, literal, are all represented by this. Values like OK, STATUS are too. + * Also, this class class may contain more arbitrary value like "BODY[HEADER.FIELDS ("DATE")]". See + * {@link ImapResponseParser}. + */ +public abstract class ImapString extends ImapElement { + private static final byte[] EMPTY_BYTES = new byte[0]; + + public static final ImapString EMPTY = + new ImapString() { + @Override + public void destroy() { + // Don't call super.destroy(). + // It's a shared object. We don't want the mDestroyed to be set on this. + } + + @Override + public String getString() { + return ""; + } + + @Override + public InputStream getAsStream() { + return new ByteArrayInputStream(EMPTY_BYTES); + } + + @Override + public String toString() { + return ""; + } + }; + + // This is used only for parsing IMAP's FETCH ENVELOPE command, in which + // en_US-like date format is used like "01-Jan-2009 11:20:39 -0800", so this should be + // handled by Locale.US + private static final SimpleDateFormat DATE_TIME_FORMAT = + new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss Z", Locale.US); + + private boolean mIsInteger; + private int mParsedInteger; + private Date mParsedDate; + + @Override + public final boolean isList() { + return false; + } + + @Override + public final boolean isString() { + return true; + } + + /** + * @return true if and only if the length of the string is larger than 0. + *

Note: IMAP NIL is considered an empty string. See {@link ImapResponseParser + * #parseBareString}. On the other hand, a quoted/literal string with value NIL (i.e. "NIL" + * and {3}\r\nNIL) is treated literally. + */ + public final boolean isEmpty() { + return getString().length() == 0; + } + + public abstract String getString(); + + public abstract InputStream getAsStream(); + + /** @return whether it can be parsed as a number. */ + public final boolean isNumber() { + if (mIsInteger) { + return true; + } + try { + mParsedInteger = Integer.parseInt(getString()); + mIsInteger = true; + return true; + } catch (NumberFormatException e) { + return false; + } + } + + /** @return value parsed as a number, or 0 if the string is not a number. */ + public final int getNumberOrZero() { + return getNumber(0); + } + + /** @return value parsed as a number, or {@code defaultValue} if the string is not a number. */ + public final int getNumber(int defaultValue) { + if (!isNumber()) { + return defaultValue; + } + return mParsedInteger; + } + + /** @return whether it can be parsed as a date using {@link #DATE_TIME_FORMAT}. */ + public final boolean isDate() { + if (mParsedDate != null) { + return true; + } + if (isEmpty()) { + return false; + } + try { + mParsedDate = DATE_TIME_FORMAT.parse(getString()); + return true; + } catch (ParseException e) { + VvmLog.w("ImapString", getString() + " can't be parsed as a date."); + return false; + } + } + + /** @return value it can be parsed as a {@link Date}, or null otherwise. */ + public final Date getDateOrNull() { + if (!isDate()) { + return null; + } + return mParsedDate; + } + + /** @return whether the value case-insensitively equals to {@code s}. */ + public final boolean is(String s) { + if (s == null) { + return false; + } + return getString().equalsIgnoreCase(s); + } + + /** @return whether the value case-insensitively starts with {@code s}. */ + public final boolean startsWith(String prefix) { + if (prefix == null) { + return false; + } + final String me = this.getString(); + if (me.length() < prefix.length()) { + return false; + } + return me.substring(0, prefix.length()).equalsIgnoreCase(prefix); + } + + // To force subclasses to implement it. + @Override + public abstract String toString(); + + @Override + public final boolean equalsForTest(ImapElement that) { + if (!super.equalsForTest(that)) { + return false; + } + ImapString thatString = (ImapString) that; + return getString().equals(thatString.getString()); + } +} diff --git a/java/com/android/voicemail/impl/mail/store/imap/ImapTempFileLiteral.java b/java/com/android/voicemail/impl/mail/store/imap/ImapTempFileLiteral.java new file mode 100644 index 000000000..ab64d8537 --- /dev/null +++ b/java/com/android/voicemail/impl/mail/store/imap/ImapTempFileLiteral.java @@ -0,0 +1,119 @@ +/* + * 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.voicemail.impl.mail.store.imap; + +import com.android.voicemail.impl.mail.FixedLengthInputStream; +import com.android.voicemail.impl.mail.TempDirectory; +import com.android.voicemail.impl.mail.utils.LogUtils; +import com.android.voicemail.impl.mail.utils.Utility; +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import org.apache.commons.io.IOUtils; + +/** Subclass of {@link ImapString} used for literals backed by a temp file. */ +public class ImapTempFileLiteral extends ImapString { + private final String TAG = "ImapTempFileLiteral"; + + /* package for test */ final File mFile; + + /** Size is purely for toString() */ + private final int mSize; + + /* package */ ImapTempFileLiteral(FixedLengthInputStream stream) throws IOException { + mSize = stream.getLength(); + mFile = File.createTempFile("imap", ".tmp", TempDirectory.getTempDirectory()); + + // Unfortunately, we can't really use deleteOnExit(), because temp filenames are random + // so it'd simply cause a memory leak. + // deleteOnExit() simply adds filenames to a static list and the list will never shrink. + // mFile.deleteOnExit(); + OutputStream out = new FileOutputStream(mFile); + IOUtils.copy(stream, out); + out.close(); + } + + /** + * Make sure we delete the temp file. + * + *

We should always be calling {@link ImapResponse#destroy()}, but it's here as a last resort. + */ + @Override + protected void finalize() throws Throwable { + try { + destroy(); + } finally { + super.finalize(); + } + } + + @Override + public InputStream getAsStream() { + checkNotDestroyed(); + try { + return new FileInputStream(mFile); + } catch (FileNotFoundException e) { + // It's probably possible if we're low on storage and the system clears the cache dir. + LogUtils.w(TAG, "ImapTempFileLiteral: Temp file not found"); + + // Return 0 byte stream as a dummy... + return new ByteArrayInputStream(new byte[0]); + } + } + + @Override + public String getString() { + checkNotDestroyed(); + try { + byte[] bytes = IOUtils.toByteArray(getAsStream()); + // Prevent crash from OOM; we've seen this, but only rarely and not reproducibly + if (bytes.length > ImapResponseParser.LITERAL_KEEP_IN_MEMORY_THRESHOLD) { + throw new IOException(); + } + return Utility.fromAscii(bytes); + } catch (IOException e) { + LogUtils.w(TAG, "ImapTempFileLiteral: Error while reading temp file", e); + return ""; + } + } + + @Override + public void destroy() { + try { + if (!isDestroyed() && mFile.exists()) { + mFile.delete(); + } + } catch (RuntimeException re) { + // Just log and ignore. + LogUtils.w(TAG, "Failed to remove temp file: " + re.getMessage()); + } + super.destroy(); + } + + @Override + public String toString() { + return String.format("{%d byte literal(file)}", mSize); + } + + public boolean tempFileExistsForTest() { + return mFile.exists(); + } +} diff --git a/java/com/android/voicemail/impl/mail/store/imap/ImapUtility.java b/java/com/android/voicemail/impl/mail/store/imap/ImapUtility.java new file mode 100644 index 000000000..a325cc295 --- /dev/null +++ b/java/com/android/voicemail/impl/mail/store/imap/ImapUtility.java @@ -0,0 +1,122 @@ +/* + * 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.voicemail.impl.mail.store.imap; + +import com.android.voicemail.impl.mail.utils.LogUtils; +import java.util.ArrayList; + +/** Utility methods for use with IMAP. */ +public class ImapUtility { + public static final String TAG = "ImapUtility"; + /** + * Apply quoting rules per IMAP RFC, quoted = DQUOTE *QUOTED-CHAR DQUOTE QUOTED-CHAR = / "\" quoted-specials quoted-specials = DQUOTE / "\" + * + *

This is used primarily for IMAP login, but might be useful elsewhere. + * + *

NOTE: Not very efficient - you may wish to preflight this, or perhaps it should check for + * trouble chars before calling the replace functions. + * + * @param s The string to be quoted. + * @return A copy of the string, having undergone quoting as described above + */ + public static String imapQuoted(String s) { + + // First, quote any backslashes by replacing \ with \\ + // regex Pattern: \\ (Java string const = \\\\) + // Substitute: \\\\ (Java string const = \\\\\\\\) + String result = s.replaceAll("\\\\", "\\\\\\\\"); + + // Then, quote any double-quotes by replacing " with \" + // regex Pattern: " (Java string const = \") + // Substitute: \\" (Java string const = \\\\\") + result = result.replaceAll("\"", "\\\\\""); + + // return string with quotes around it + return "\"" + result + "\""; + } + + /** + * Gets all of the values in a sequence set per RFC 3501. Any ranges are expanded into a list of + * individual numbers. If the set is invalid, an empty array is returned. + * + *

+   * sequence-number = nz-number / "*"
+   * sequence-range  = sequence-number ":" sequence-number
+   * sequence-set    = (sequence-number / sequence-range) *("," sequence-set)
+   * 
+ */ + public static String[] getImapSequenceValues(String set) { + ArrayList list = new ArrayList(); + if (set != null) { + String[] setItems = set.split(","); + for (String item : setItems) { + if (item.indexOf(':') == -1) { + // simple item + try { + Integer.parseInt(item); // Don't need the value; just ensure it's valid + list.add(item); + } catch (NumberFormatException e) { + LogUtils.d(TAG, "Invalid UID value", e); + } + } else { + // range + for (String rangeItem : getImapRangeValues(item)) { + list.add(rangeItem); + } + } + } + } + String[] stringList = new String[list.size()]; + return list.toArray(stringList); + } + + /** + * Expand the given number range into a list of individual numbers. If the range is not valid, an + * empty array is returned. + * + *
+   * sequence-number = nz-number / "*"
+   * sequence-range  = sequence-number ":" sequence-number
+   * sequence-set    = (sequence-number / sequence-range) *("," sequence-set)
+   * 
+ */ + public static String[] getImapRangeValues(String range) { + ArrayList list = new ArrayList(); + try { + if (range != null) { + int colonPos = range.indexOf(':'); + if (colonPos > 0) { + int first = Integer.parseInt(range.substring(0, colonPos)); + int second = Integer.parseInt(range.substring(colonPos + 1)); + if (first < second) { + for (int i = first; i <= second; i++) { + list.add(Integer.toString(i)); + } + } else { + for (int i = first; i >= second; i--) { + list.add(Integer.toString(i)); + } + } + } + } + } catch (NumberFormatException e) { + LogUtils.d(TAG, "Invalid range value", e); + } + String[] stringList = new String[list.size()]; + return list.toArray(stringList); + } +} diff --git a/java/com/android/voicemail/impl/mail/utility/CountingOutputStream.java b/java/com/android/voicemail/impl/mail/utility/CountingOutputStream.java new file mode 100644 index 000000000..c3586105f --- /dev/null +++ b/java/com/android/voicemail/impl/mail/utility/CountingOutputStream.java @@ -0,0 +1,48 @@ +/* + * 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.voicemail.impl.mail.utility; + +import java.io.IOException; +import java.io.OutputStream; + +/** + * A simple pass-thru OutputStream that also counts how many bytes are written to it and makes that + * count available to callers. + */ +public class CountingOutputStream extends OutputStream { + private long mCount; + private final OutputStream mOutputStream; + + public CountingOutputStream(OutputStream outputStream) { + mOutputStream = outputStream; + } + + public long getCount() { + return mCount; + } + + @Override + public void write(byte[] buffer, int offset, int count) throws IOException { + mOutputStream.write(buffer, offset, count); + mCount += count; + } + + @Override + public void write(int oneByte) throws IOException { + mOutputStream.write(oneByte); + mCount++; + } +} diff --git a/java/com/android/voicemail/impl/mail/utility/EOLConvertingOutputStream.java b/java/com/android/voicemail/impl/mail/utility/EOLConvertingOutputStream.java new file mode 100644 index 000000000..72649ac4d --- /dev/null +++ b/java/com/android/voicemail/impl/mail/utility/EOLConvertingOutputStream.java @@ -0,0 +1,48 @@ +/* + * 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.voicemail.impl.mail.utility; + +import java.io.FilterOutputStream; +import java.io.IOException; +import java.io.OutputStream; + +public class EOLConvertingOutputStream extends FilterOutputStream { + int lastChar; + + public EOLConvertingOutputStream(OutputStream out) { + super(out); + } + + @Override + public void write(int oneByte) throws IOException { + if (oneByte == '\n') { + if (lastChar != '\r') { + super.write('\r'); + } + } + super.write(oneByte); + lastChar = oneByte; + } + + @Override + public void flush() throws IOException { + if (lastChar == '\r') { + super.write('\n'); + lastChar = '\n'; + } + super.flush(); + } +} diff --git a/java/com/android/voicemail/impl/mail/utils/LogUtils.java b/java/com/android/voicemail/impl/mail/utils/LogUtils.java new file mode 100644 index 000000000..f6c3c6ba3 --- /dev/null +++ b/java/com/android/voicemail/impl/mail/utils/LogUtils.java @@ -0,0 +1,345 @@ +/** + * 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.voicemail.impl.mail.utils; + +import android.net.Uri; +import android.support.annotation.VisibleForTesting; +import android.text.TextUtils; +import android.util.Log; +import com.android.voicemail.impl.VvmLog; +import java.util.List; + +public class LogUtils { + public static final String TAG = "Email Log"; + + private static final String ACCOUNT_PREFIX = "account:"; + + /** Priority constant for the println method; use LogUtils.v. */ + public static final int VERBOSE = Log.VERBOSE; + + /** Priority constant for the println method; use LogUtils.d. */ + public static final int DEBUG = Log.DEBUG; + + /** Priority constant for the println method; use LogUtils.i. */ + public static final int INFO = Log.INFO; + + /** Priority constant for the println method; use LogUtils.w. */ + public static final int WARN = Log.WARN; + + /** Priority constant for the println method; use LogUtils.e. */ + public static final int ERROR = Log.ERROR; + + /** + * Used to enable/disable logging that we don't want included in production releases. This should + * be set to DEBUG for production releases, and VERBOSE for internal builds. + */ + private static final int MAX_ENABLED_LOG_LEVEL = DEBUG; + + private static Boolean sDebugLoggingEnabledForTests = null; + + /** Enable debug logging for unit tests. */ + @VisibleForTesting + public static void setDebugLoggingEnabledForTests(boolean enabled) { + setDebugLoggingEnabledForTestsInternal(enabled); + } + + protected static void setDebugLoggingEnabledForTestsInternal(boolean enabled) { + sDebugLoggingEnabledForTests = Boolean.valueOf(enabled); + } + + /** Returns true if the build configuration prevents debug logging. */ + @VisibleForTesting + public static boolean buildPreventsDebugLogging() { + return MAX_ENABLED_LOG_LEVEL > VERBOSE; + } + + /** Returns a boolean indicating whether debug logging is enabled. */ + protected static boolean isDebugLoggingEnabled(String tag) { + if (buildPreventsDebugLogging()) { + return false; + } + if (sDebugLoggingEnabledForTests != null) { + return sDebugLoggingEnabledForTests.booleanValue(); + } + return Log.isLoggable(tag, Log.DEBUG) || Log.isLoggable(TAG, Log.DEBUG); + } + + /** + * Returns a String for the specified content provider uri. This will do sanitation of the uri to + * remove PII if debug logging is not enabled. + */ + public static String contentUriToString(final Uri uri) { + return contentUriToString(TAG, uri); + } + + /** + * Returns a String for the specified content provider uri. This will do sanitation of the uri to + * remove PII if debug logging is not enabled. + */ + public static String contentUriToString(String tag, Uri uri) { + if (isDebugLoggingEnabled(tag)) { + // Debug logging has been enabled, so log the uri as is + return uri.toString(); + } else { + // Debug logging is not enabled, we want to remove the email address from the uri. + List pathSegments = uri.getPathSegments(); + + Uri.Builder builder = + new Uri.Builder() + .scheme(uri.getScheme()) + .authority(uri.getAuthority()) + .query(uri.getQuery()) + .fragment(uri.getFragment()); + + // This assumes that the first path segment is the account + final String account = pathSegments.get(0); + + builder = builder.appendPath(sanitizeAccountName(account)); + for (int i = 1; i < pathSegments.size(); i++) { + builder.appendPath(pathSegments.get(i)); + } + return builder.toString(); + } + } + + /** Sanitizes an account name. If debug logging is not enabled, a sanitized name is returned. */ + public static String sanitizeAccountName(String accountName) { + if (TextUtils.isEmpty(accountName)) { + return ""; + } + + return ACCOUNT_PREFIX + sanitizeName(TAG, accountName); + } + + public static String sanitizeName(final String tag, final String name) { + if (TextUtils.isEmpty(name)) { + return ""; + } + + if (isDebugLoggingEnabled(tag)) { + return name; + } + + return String.valueOf(name.hashCode()); + } + + /** + * Checks to see whether or not a log for the specified tag is loggable at the specified level. + */ + public static boolean isLoggable(String tag, int level) { + if (MAX_ENABLED_LOG_LEVEL > level) { + return false; + } + return Log.isLoggable(tag, level) || Log.isLoggable(TAG, level); + } + + /** + * Send a {@link #VERBOSE} log message. + * + * @param tag Used to identify the source of a log message. It usually identifies the class or + * activity where the log call occurs. + * @param format the format string (see {@link java.util.Formatter#format}) + * @param args the list of arguments passed to the formatter. If there are more arguments than + * required by {@code format}, additional arguments are ignored. + */ + public static void v(String tag, String format, Object... args) { + if (isLoggable(tag, VERBOSE)) { + VvmLog.v(tag, String.format(format, args)); + } + } + + /** + * Send a {@link #VERBOSE} log message. + * + * @param tag Used to identify the source of a log message. It usually identifies the class or + * activity where the log call occurs. + * @param tr An exception to log + * @param format the format string (see {@link java.util.Formatter#format}) + * @param args the list of arguments passed to the formatter. If there are more arguments than + * required by {@code format}, additional arguments are ignored. + */ + public static void v(String tag, Throwable tr, String format, Object... args) { + if (isLoggable(tag, VERBOSE)) { + VvmLog.v(tag, String.format(format, args), tr); + } + } + + /** + * Send a {@link #DEBUG} log message. + * + * @param tag Used to identify the source of a log message. It usually identifies the class or + * activity where the log call occurs. + * @param format the format string (see {@link java.util.Formatter#format}) + * @param args the list of arguments passed to the formatter. If there are more arguments than + * required by {@code format}, additional arguments are ignored. + */ + public static void d(String tag, String format, Object... args) { + if (isLoggable(tag, DEBUG)) { + VvmLog.d(tag, String.format(format, args)); + } + } + + /** + * Send a {@link #DEBUG} log message. + * + * @param tag Used to identify the source of a log message. It usually identifies the class or + * activity where the log call occurs. + * @param tr An exception to log + * @param format the format string (see {@link java.util.Formatter#format}) + * @param args the list of arguments passed to the formatter. If there are more arguments than + * required by {@code format}, additional arguments are ignored. + */ + public static void d(String tag, Throwable tr, String format, Object... args) { + if (isLoggable(tag, DEBUG)) { + VvmLog.d(tag, String.format(format, args), tr); + } + } + + /** + * Send a {@link #INFO} log message. + * + * @param tag Used to identify the source of a log message. It usually identifies the class or + * activity where the log call occurs. + * @param format the format string (see {@link java.util.Formatter#format}) + * @param args the list of arguments passed to the formatter. If there are more arguments than + * required by {@code format}, additional arguments are ignored. + */ + public static void i(String tag, String format, Object... args) { + if (isLoggable(tag, INFO)) { + VvmLog.i(tag, String.format(format, args)); + } + } + + /** + * Send a {@link #INFO} log message. + * + * @param tag Used to identify the source of a log message. It usually identifies the class or + * activity where the log call occurs. + * @param tr An exception to log + * @param format the format string (see {@link java.util.Formatter#format}) + * @param args the list of arguments passed to the formatter. If there are more arguments than + * required by {@code format}, additional arguments are ignored. + */ + public static void i(String tag, Throwable tr, String format, Object... args) { + if (isLoggable(tag, INFO)) { + VvmLog.i(tag, String.format(format, args), tr); + } + } + + /** + * Send a {@link #WARN} log message. + * + * @param tag Used to identify the source of a log message. It usually identifies the class or + * activity where the log call occurs. + * @param format the format string (see {@link java.util.Formatter#format}) + * @param args the list of arguments passed to the formatter. If there are more arguments than + * required by {@code format}, additional arguments are ignored. + */ + public static void w(String tag, String format, Object... args) { + if (isLoggable(tag, WARN)) { + VvmLog.w(tag, String.format(format, args)); + } + } + + /** + * Send a {@link #WARN} log message. + * + * @param tag Used to identify the source of a log message. It usually identifies the class or + * activity where the log call occurs. + * @param tr An exception to log + * @param format the format string (see {@link java.util.Formatter#format}) + * @param args the list of arguments passed to the formatter. If there are more arguments than + * required by {@code format}, additional arguments are ignored. + */ + public static void w(String tag, Throwable tr, String format, Object... args) { + if (isLoggable(tag, WARN)) { + VvmLog.w(tag, String.format(format, args), tr); + } + } + + /** + * Send a {@link #ERROR} log message. + * + * @param tag Used to identify the source of a log message. It usually identifies the class or + * activity where the log call occurs. + * @param format the format string (see {@link java.util.Formatter#format}) + * @param args the list of arguments passed to the formatter. If there are more arguments than + * required by {@code format}, additional arguments are ignored. + */ + public static void e(String tag, String format, Object... args) { + if (isLoggable(tag, ERROR)) { + VvmLog.e(tag, String.format(format, args)); + } + } + + /** + * Send a {@link #ERROR} log message. + * + * @param tag Used to identify the source of a log message. It usually identifies the class or + * activity where the log call occurs. + * @param tr An exception to log + * @param format the format string (see {@link java.util.Formatter#format}) + * @param args the list of arguments passed to the formatter. If there are more arguments than + * required by {@code format}, additional arguments are ignored. + */ + public static void e(String tag, Throwable tr, String format, Object... args) { + if (isLoggable(tag, ERROR)) { + VvmLog.e(tag, String.format(format, args), tr); + } + } + + /** + * What a Terrible Failure: Report a condition that should never happen. The error will always be + * logged at level ASSERT with the call stack. Depending on system configuration, a report may be + * added to the {@link android.os.DropBoxManager} and/or the process may be terminated immediately + * with an error dialog. + * + * @param tag Used to identify the source of a log message. It usually identifies the class or + * activity where the log call occurs. + * @param format the format string (see {@link java.util.Formatter#format}) + * @param args the list of arguments passed to the formatter. If there are more arguments than + * required by {@code format}, additional arguments are ignored. + */ + public static void wtf(String tag, String format, Object... args) { + VvmLog.wtf(tag, String.format(format, args), new Error()); + } + + /** + * What a Terrible Failure: Report a condition that should never happen. The error will always be + * logged at level ASSERT with the call stack. Depending on system configuration, a report may be + * added to the {@link android.os.DropBoxManager} and/or the process may be terminated immediately + * with an error dialog. + * + * @param tag Used to identify the source of a log message. It usually identifies the class or + * activity where the log call occurs. + * @param tr An exception to log + * @param format the format string (see {@link java.util.Formatter#format}) + * @param args the list of arguments passed to the formatter. If there are more arguments than + * required by {@code format}, additional arguments are ignored. + */ + public static void wtf(String tag, Throwable tr, String format, Object... args) { + VvmLog.wtf(tag, String.format(format, args), tr); + } + + public static String byteToHex(int b) { + return byteToHex(new StringBuilder(), b).toString(); + } + + public static StringBuilder byteToHex(StringBuilder sb, int b) { + b &= 0xFF; + sb.append("0123456789ABCDEF".charAt(b >> 4)); + sb.append("0123456789ABCDEF".charAt(b & 0xF)); + return sb; + } +} diff --git a/java/com/android/voicemail/impl/mail/utils/Utility.java b/java/com/android/voicemail/impl/mail/utils/Utility.java new file mode 100644 index 000000000..4db1681fb --- /dev/null +++ b/java/com/android/voicemail/impl/mail/utils/Utility.java @@ -0,0 +1,76 @@ +/** + * 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.voicemail.impl.mail.utils; + +import java.io.ByteArrayInputStream; +import java.nio.ByteBuffer; +import java.nio.CharBuffer; +import java.nio.charset.Charset; + +/** Simple utility methods used in email functions. */ +public class Utility { + public static final Charset ASCII = Charset.forName("US-ASCII"); + + public static final String[] EMPTY_STRINGS = new String[0]; + + /** + * Returns a concatenated string containing the output of every Object's toString() method, each + * separated by the given separator character. + */ + public static String combine(Object[] parts, char separator) { + if (parts == null) { + return null; + } + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < parts.length; i++) { + sb.append(parts[i].toString()); + if (i < parts.length - 1) { + sb.append(separator); + } + } + return sb.toString(); + } + + /** Converts a String to ASCII bytes */ + public static byte[] toAscii(String s) { + return encode(ASCII, s); + } + + /** Builds a String from ASCII bytes */ + public static String fromAscii(byte[] b) { + return decode(ASCII, b); + } + + private static byte[] encode(Charset charset, String s) { + if (s == null) { + return null; + } + final ByteBuffer buffer = charset.encode(CharBuffer.wrap(s)); + final byte[] bytes = new byte[buffer.limit()]; + buffer.get(bytes); + return bytes; + } + + private static String decode(Charset charset, byte[] b) { + if (b == null) { + return null; + } + final CharBuffer cb = charset.decode(ByteBuffer.wrap(b)); + return new String(cb.array(), 0, cb.length()); + } + + public static ByteArrayInputStream streamFromAsciiString(String ascii) { + return new ByteArrayInputStream(toAscii(ascii)); + } +} diff --git a/java/com/android/voicemail/impl/protocol/CvvmProtocol.java b/java/com/android/voicemail/impl/protocol/CvvmProtocol.java new file mode 100644 index 000000000..a4b54f68c --- /dev/null +++ b/java/com/android/voicemail/impl/protocol/CvvmProtocol.java @@ -0,0 +1,59 @@ +/* + * 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.voicemail.impl.protocol; + +import android.content.Context; +import android.telecom.PhoneAccountHandle; +import com.android.voicemail.impl.OmtpConstants; +import com.android.voicemail.impl.sms.OmtpCvvmMessageSender; +import com.android.voicemail.impl.sms.OmtpMessageSender; + +/** + * A flavor of OMTP protocol with a different mobile originated (MO) format + * + *

Used by carriers such as T-Mobile + */ +public class CvvmProtocol extends VisualVoicemailProtocol { + + private static String IMAP_CHANGE_TUI_PWD_FORMAT = "CHANGE_TUI_PWD PWD=%1$s OLD_PWD=%2$s"; + private static String IMAP_CHANGE_VM_LANG_FORMAT = "CHANGE_VM_LANG Lang=%1$s"; + private static String IMAP_CLOSE_NUT = "CLOSE_NUT"; + + @Override + public OmtpMessageSender createMessageSender( + Context context, + PhoneAccountHandle phoneAccountHandle, + short applicationPort, + String destinationNumber) { + return new OmtpCvvmMessageSender( + context, phoneAccountHandle, applicationPort, destinationNumber); + } + + @Override + public String getCommand(String command) { + if (command == OmtpConstants.IMAP_CHANGE_TUI_PWD_FORMAT) { + return IMAP_CHANGE_TUI_PWD_FORMAT; + } + if (command == OmtpConstants.IMAP_CLOSE_NUT) { + return IMAP_CLOSE_NUT; + } + if (command == OmtpConstants.IMAP_CHANGE_VM_LANG_FORMAT) { + return IMAP_CHANGE_VM_LANG_FORMAT; + } + return super.getCommand(command); + } +} diff --git a/java/com/android/voicemail/impl/protocol/OmtpProtocol.java b/java/com/android/voicemail/impl/protocol/OmtpProtocol.java new file mode 100644 index 000000000..27aab8a7c --- /dev/null +++ b/java/com/android/voicemail/impl/protocol/OmtpProtocol.java @@ -0,0 +1,42 @@ +/* + * 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.voicemail.impl.protocol; + +import android.content.Context; +import android.telecom.PhoneAccountHandle; +import com.android.voicemail.impl.OmtpConstants; +import com.android.voicemail.impl.sms.OmtpMessageSender; +import com.android.voicemail.impl.sms.OmtpStandardMessageSender; + +public class OmtpProtocol extends VisualVoicemailProtocol { + + @Override + public OmtpMessageSender createMessageSender( + Context context, + PhoneAccountHandle phoneAccountHandle, + short applicationPort, + String destinationNumber) { + return new OmtpStandardMessageSender( + context, + phoneAccountHandle, + applicationPort, + destinationNumber, + OmtpConstants.CLIENT_TYPE_GOOGLE_10, + OmtpConstants.PROTOCOL_VERSION1_1, + null /*clientPrefix*/); + } +} diff --git a/java/com/android/voicemail/impl/protocol/ProtocolHelper.java b/java/com/android/voicemail/impl/protocol/ProtocolHelper.java new file mode 100644 index 000000000..4d2e7cce4 --- /dev/null +++ b/java/com/android/voicemail/impl/protocol/ProtocolHelper.java @@ -0,0 +1,44 @@ +/* + * 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.voicemail.impl.protocol; + +import android.text.TextUtils; +import com.android.voicemail.impl.OmtpVvmCarrierConfigHelper; +import com.android.voicemail.impl.VvmLog; +import com.android.voicemail.impl.sms.OmtpMessageSender; + +public class ProtocolHelper { + + private static final String TAG = "ProtocolHelper"; + + public static OmtpMessageSender getMessageSender( + VisualVoicemailProtocol protocol, OmtpVvmCarrierConfigHelper config) { + + int applicationPort = config.getApplicationPort(); + String destinationNumber = config.getDestinationNumber(); + if (TextUtils.isEmpty(destinationNumber)) { + VvmLog.w(TAG, "No destination number for this carrier."); + return null; + } + + return protocol.createMessageSender( + config.getContext(), + config.getPhoneAccountHandle(), + (short) applicationPort, + destinationNumber); + } +} diff --git a/java/com/android/voicemail/impl/protocol/VisualVoicemailProtocol.java b/java/com/android/voicemail/impl/protocol/VisualVoicemailProtocol.java new file mode 100644 index 000000000..6cf82f1b8 --- /dev/null +++ b/java/com/android/voicemail/impl/protocol/VisualVoicemailProtocol.java @@ -0,0 +1,106 @@ +/* + * 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.voicemail.impl.protocol; + +import android.app.PendingIntent; +import android.content.Context; +import android.os.Bundle; +import android.support.annotation.Nullable; +import android.telecom.PhoneAccountHandle; +import com.android.voicemail.impl.ActivationTask; +import com.android.voicemail.impl.DefaultOmtpEventHandler; +import com.android.voicemail.impl.OmtpConstants; +import com.android.voicemail.impl.OmtpEvents; +import com.android.voicemail.impl.OmtpVvmCarrierConfigHelper; +import com.android.voicemail.impl.VoicemailStatus; +import com.android.voicemail.impl.sms.OmtpMessageSender; +import com.android.voicemail.impl.sms.StatusMessage; + +public abstract class VisualVoicemailProtocol { + + /** Activation should cause the carrier to respond with a STATUS SMS. */ + public void startActivation(OmtpVvmCarrierConfigHelper config, PendingIntent sentIntent) { + OmtpMessageSender messageSender = ProtocolHelper.getMessageSender(this, config); + if (messageSender != null) { + messageSender.requestVvmActivation(sentIntent); + } + } + + public void startDeactivation(OmtpVvmCarrierConfigHelper config) { + OmtpMessageSender messageSender = ProtocolHelper.getMessageSender(this, config); + if (messageSender != null) { + messageSender.requestVvmDeactivation(null); + } + } + + public boolean supportsProvisioning() { + return false; + } + + public void startProvisioning( + ActivationTask task, + PhoneAccountHandle handle, + OmtpVvmCarrierConfigHelper config, + VoicemailStatus.Editor editor, + StatusMessage message, + Bundle data) { + // Do nothing + } + + public void requestStatus(OmtpVvmCarrierConfigHelper config, @Nullable PendingIntent sentIntent) { + OmtpMessageSender messageSender = ProtocolHelper.getMessageSender(this, config); + if (messageSender != null) { + messageSender.requestVvmStatus(sentIntent); + } + } + + public abstract OmtpMessageSender createMessageSender( + Context context, + PhoneAccountHandle phoneAccountHandle, + short applicationPort, + String destinationNumber); + + /** + * Translate an OMTP IMAP command to the protocol specific one. For example, changing the TUI + * password on OMTP is XCHANGE_TUI_PWD, but on CVVM and VVM3 it is CHANGE_TUI_PWD. + * + * @param command A String command in {@link OmtpConstants}, the exact + * instance should be used instead of its' value. + * @returns Translated command, or {@code null} if not available in this protocol + */ + public String getCommand(String command) { + return command; + } + + public void handleEvent( + Context context, + OmtpVvmCarrierConfigHelper config, + VoicemailStatus.Editor status, + OmtpEvents event) { + DefaultOmtpEventHandler.handleEvent(context, config, status, event); + } + + /** + * Given an VVM SMS with an unknown {@code event}, let the protocol attempt to translate it into + * an equivalent STATUS SMS. Returns {@code null} if it cannot be translated. + */ + @Nullable + public Bundle translateStatusSmsBundle( + OmtpVvmCarrierConfigHelper config, String event, Bundle data) { + return null; + } +} diff --git a/java/com/android/voicemail/impl/protocol/VisualVoicemailProtocolFactory.java b/java/com/android/voicemail/impl/protocol/VisualVoicemailProtocolFactory.java new file mode 100644 index 000000000..056fb2eaf --- /dev/null +++ b/java/com/android/voicemail/impl/protocol/VisualVoicemailProtocolFactory.java @@ -0,0 +1,47 @@ +/* + * 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.voicemail.impl.protocol; + +import android.content.res.Resources; +import android.support.annotation.Nullable; +import android.telephony.TelephonyManager; +import com.android.voicemail.impl.VvmLog; + +public class VisualVoicemailProtocolFactory { + + private static final String TAG = "VvmProtocolFactory"; + + private static final String VVM_TYPE_VVM3 = "vvm_type_vvm3"; + + @Nullable + public static VisualVoicemailProtocol create(Resources resources, String type) { + if (type == null) { + return null; + } + switch (type) { + case TelephonyManager.VVM_TYPE_OMTP: + return new OmtpProtocol(); + case TelephonyManager.VVM_TYPE_CVVM: + return new CvvmProtocol(); + case VVM_TYPE_VVM3: + return new Vvm3Protocol(); + default: + VvmLog.e(TAG, "Unexpected visual voicemail type: " + type); + } + return null; + } +} diff --git a/java/com/android/voicemail/impl/protocol/Vvm3EventHandler.java b/java/com/android/voicemail/impl/protocol/Vvm3EventHandler.java new file mode 100644 index 000000000..8bc3cc21c --- /dev/null +++ b/java/com/android/voicemail/impl/protocol/Vvm3EventHandler.java @@ -0,0 +1,307 @@ +/* + * 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.voicemail.impl.protocol; + +import android.content.Context; +import android.provider.VoicemailContract.Status; +import android.support.annotation.IntDef; +import android.telecom.PhoneAccountHandle; +import com.android.voicemail.impl.DefaultOmtpEventHandler; +import com.android.voicemail.impl.OmtpEvents; +import com.android.voicemail.impl.OmtpEvents.Type; +import com.android.voicemail.impl.OmtpVvmCarrierConfigHelper; +import com.android.voicemail.impl.VoicemailStatus; +import com.android.voicemail.impl.VvmLog; +import com.android.voicemail.impl.settings.VoicemailChangePinActivity; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +/** + * Handles {@link OmtpEvents} when {@link Vvm3Protocol} is being used. This handler writes custom + * error codes into the voicemail status table so support on the dialer side is required. + * + *

TODO(b/29577838) disable VVM3 by default so support on system dialer can be ensured. + */ +public class Vvm3EventHandler { + + private static final String TAG = "Vvm3EventHandler"; + + @Retention(RetentionPolicy.SOURCE) + @IntDef({ + VMS_DNS_FAILURE, + VMG_DNS_FAILURE, + SPG_DNS_FAILURE, + VMS_NO_CELLULAR, + VMG_NO_CELLULAR, + SPG_NO_CELLULAR, + VMS_TIMEOUT, + VMG_TIMEOUT, + STATUS_SMS_TIMEOUT, + SUBSCRIBER_BLOCKED, + UNKNOWN_USER, + UNKNOWN_DEVICE, + INVALID_PASSWORD, + MAILBOX_NOT_INITIALIZED, + SERVICE_NOT_PROVISIONED, + SERVICE_NOT_ACTIVATED, + USER_BLOCKED, + IMAP_GETQUOTA_ERROR, + IMAP_SELECT_ERROR, + IMAP_ERROR, + VMG_INTERNAL_ERROR, + VMG_DB_ERROR, + VMG_COMMUNICATION_ERROR, + SPG_URL_NOT_FOUND, + VMG_UNKNOWN_ERROR, + PIN_NOT_SET + }) + public @interface ErrorCode {} + + public static final int VMS_DNS_FAILURE = -9001; + public static final int VMG_DNS_FAILURE = -9002; + public static final int SPG_DNS_FAILURE = -9003; + public static final int VMS_NO_CELLULAR = -9004; + public static final int VMG_NO_CELLULAR = -9005; + public static final int SPG_NO_CELLULAR = -9006; + public static final int VMS_TIMEOUT = -9007; + public static final int VMG_TIMEOUT = -9008; + public static final int STATUS_SMS_TIMEOUT = -9009; + + public static final int SUBSCRIBER_BLOCKED = -9990; + public static final int UNKNOWN_USER = -9991; + public static final int UNKNOWN_DEVICE = -9992; + public static final int INVALID_PASSWORD = -9993; + public static final int MAILBOX_NOT_INITIALIZED = -9994; + public static final int SERVICE_NOT_PROVISIONED = -9995; + public static final int SERVICE_NOT_ACTIVATED = -9996; + public static final int USER_BLOCKED = -9998; + public static final int IMAP_GETQUOTA_ERROR = -9997; + public static final int IMAP_SELECT_ERROR = -9989; + public static final int IMAP_ERROR = -9999; + + public static final int VMG_INTERNAL_ERROR = -101; + public static final int VMG_DB_ERROR = -102; + public static final int VMG_COMMUNICATION_ERROR = -103; + public static final int SPG_URL_NOT_FOUND = -301; + + // Non VVM3 codes: + public static final int VMG_UNKNOWN_ERROR = -1; + public static final int PIN_NOT_SET = -100; + // STATUS SMS returned st=U and rc!=2. The user cannot be provisioned and must contact customer + // support. + public static final int SUBSCRIBER_UNKNOWN = -99; + + public static void handleEvent( + Context context, + OmtpVvmCarrierConfigHelper config, + VoicemailStatus.Editor status, + OmtpEvents event) { + boolean handled = false; + switch (event.getType()) { + case Type.CONFIGURATION: + handled = handleConfigurationEvent(context, status, event); + break; + case Type.DATA_CHANNEL: + handled = handleDataChannelEvent(status, event); + break; + case Type.NOTIFICATION_CHANNEL: + handled = handleNotificationChannelEvent(status, event); + break; + case Type.OTHER: + handled = handleOtherEvent(status, event); + break; + default: + VvmLog.wtf(TAG, "invalid event type " + event.getType() + " for " + event); + } + if (!handled) { + DefaultOmtpEventHandler.handleEvent(context, config, status, event); + } + } + + private static boolean handleConfigurationEvent( + Context context, VoicemailStatus.Editor status, OmtpEvents event) { + switch (event) { + case CONFIG_REQUEST_STATUS_SUCCESS: + if (!isPinRandomized(context, status.getPhoneAccountHandle())) { + return false; + } else { + postError(status, PIN_NOT_SET); + } + break; + case CONFIG_ACTIVATING_SUBSEQUENT: + if (isPinRandomized(context, status.getPhoneAccountHandle())) { + status.setConfigurationState(PIN_NOT_SET); + } else { + status.setConfigurationState(Status.CONFIGURATION_STATE_OK); + } + status + .setNotificationChannelState(Status.NOTIFICATION_CHANNEL_STATE_OK) + .setDataChannelState(Status.DATA_CHANNEL_STATE_OK) + .apply(); + break; + case CONFIG_DEFAULT_PIN_REPLACED: + postError(status, PIN_NOT_SET); + break; + case CONFIG_STATUS_SMS_TIME_OUT: + postError(status, STATUS_SMS_TIMEOUT); + break; + default: + return false; + } + return true; + } + + private static boolean handleDataChannelEvent(VoicemailStatus.Editor status, OmtpEvents event) { + switch (event) { + case DATA_NO_CONNECTION: + case DATA_NO_CONNECTION_CELLULAR_REQUIRED: + case DATA_ALL_SOCKET_CONNECTION_FAILED: + postError(status, VMS_NO_CELLULAR); + break; + case DATA_SSL_INVALID_HOST_NAME: + case DATA_CANNOT_ESTABLISH_SSL_SESSION: + case DATA_IOE_ON_OPEN: + postError(status, VMS_TIMEOUT); + break; + case DATA_CANNOT_RESOLVE_HOST_ON_NETWORK: + postError(status, VMS_DNS_FAILURE); + break; + case DATA_BAD_IMAP_CREDENTIAL: + postError(status, IMAP_ERROR); + break; + case DATA_AUTH_UNKNOWN_USER: + postError(status, UNKNOWN_USER); + break; + case DATA_AUTH_UNKNOWN_DEVICE: + postError(status, UNKNOWN_DEVICE); + break; + case DATA_AUTH_INVALID_PASSWORD: + postError(status, INVALID_PASSWORD); + break; + case DATA_AUTH_MAILBOX_NOT_INITIALIZED: + postError(status, MAILBOX_NOT_INITIALIZED); + break; + case DATA_AUTH_SERVICE_NOT_PROVISIONED: + postError(status, SERVICE_NOT_PROVISIONED); + break; + case DATA_AUTH_SERVICE_NOT_ACTIVATED: + postError(status, SERVICE_NOT_ACTIVATED); + break; + case DATA_AUTH_USER_IS_BLOCKED: + postError(status, USER_BLOCKED); + break; + case DATA_REJECTED_SERVER_RESPONSE: + case DATA_INVALID_INITIAL_SERVER_RESPONSE: + case DATA_SSL_EXCEPTION: + postError(status, IMAP_ERROR); + break; + default: + return false; + } + return true; + } + + private static boolean handleNotificationChannelEvent( + VoicemailStatus.Editor unusedStatus, OmtpEvents unusedEvent) { + return false; + } + + private static boolean handleOtherEvent(VoicemailStatus.Editor status, OmtpEvents event) { + switch (event) { + case VVM3_NEW_USER_SETUP_FAILED: + postError(status, MAILBOX_NOT_INITIALIZED); + break; + case VVM3_VMG_DNS_FAILURE: + postError(status, VMG_DNS_FAILURE); + break; + case VVM3_SPG_DNS_FAILURE: + postError(status, SPG_DNS_FAILURE); + break; + case VVM3_VMG_CONNECTION_FAILED: + postError(status, VMG_NO_CELLULAR); + break; + case VVM3_SPG_CONNECTION_FAILED: + postError(status, SPG_NO_CELLULAR); + break; + case VVM3_VMG_TIMEOUT: + postError(status, VMG_TIMEOUT); + break; + case VVM3_SUBSCRIBER_PROVISIONED: + postError(status, SERVICE_NOT_ACTIVATED); + break; + case VVM3_SUBSCRIBER_BLOCKED: + postError(status, SUBSCRIBER_BLOCKED); + break; + case VVM3_SUBSCRIBER_UNKNOWN: + postError(status, SUBSCRIBER_UNKNOWN); + break; + default: + return false; + } + return true; + } + + private static void postError(VoicemailStatus.Editor editor, @ErrorCode int errorCode) { + switch (errorCode) { + case VMG_DNS_FAILURE: + case SPG_DNS_FAILURE: + case VMG_NO_CELLULAR: + case SPG_NO_CELLULAR: + case VMG_TIMEOUT: + case SUBSCRIBER_BLOCKED: + case UNKNOWN_USER: + case UNKNOWN_DEVICE: + case INVALID_PASSWORD: + case MAILBOX_NOT_INITIALIZED: + case SERVICE_NOT_PROVISIONED: + case SERVICE_NOT_ACTIVATED: + case USER_BLOCKED: + case VMG_UNKNOWN_ERROR: + case SPG_URL_NOT_FOUND: + case VMG_INTERNAL_ERROR: + case VMG_DB_ERROR: + case VMG_COMMUNICATION_ERROR: + case PIN_NOT_SET: + case SUBSCRIBER_UNKNOWN: + editor.setConfigurationState(errorCode); + break; + case VMS_NO_CELLULAR: + case VMS_DNS_FAILURE: + case VMS_TIMEOUT: + case IMAP_GETQUOTA_ERROR: + case IMAP_SELECT_ERROR: + case IMAP_ERROR: + editor.setDataChannelState(errorCode); + break; + case STATUS_SMS_TIMEOUT: + editor.setNotificationChannelState(errorCode); + break; + default: + VvmLog.wtf(TAG, "unknown error code: " + errorCode); + } + editor.apply(); + } + + private static boolean isPinRandomized(Context context, PhoneAccountHandle phoneAccountHandle) { + if (phoneAccountHandle == null) { + // This should never happen. + VvmLog.e(TAG, "status editor has null phone account handle"); + return false; + } + return VoicemailChangePinActivity.isDefaultOldPinSet(context, phoneAccountHandle); + } +} diff --git a/java/com/android/voicemail/impl/protocol/Vvm3Protocol.java b/java/com/android/voicemail/impl/protocol/Vvm3Protocol.java new file mode 100644 index 000000000..f293a4cdb --- /dev/null +++ b/java/com/android/voicemail/impl/protocol/Vvm3Protocol.java @@ -0,0 +1,305 @@ +/* + * 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.voicemail.impl.protocol; + +import android.annotation.TargetApi; +import android.app.PendingIntent; +import android.content.Context; +import android.net.Network; +import android.os.Build.VERSION_CODES; +import android.os.Bundle; +import android.support.annotation.Nullable; +import android.telecom.PhoneAccountHandle; +import android.text.TextUtils; +import com.android.voicemail.impl.ActivationTask; +import com.android.voicemail.impl.OmtpConstants; +import com.android.voicemail.impl.OmtpEvents; +import com.android.voicemail.impl.OmtpVvmCarrierConfigHelper; +import com.android.voicemail.impl.VisualVoicemailPreferences; +import com.android.voicemail.impl.VoicemailStatus; +import com.android.voicemail.impl.VvmLog; +import com.android.voicemail.impl.imap.ImapHelper; +import com.android.voicemail.impl.imap.ImapHelper.InitializingException; +import com.android.voicemail.impl.mail.MessagingException; +import com.android.voicemail.impl.settings.VisualVoicemailSettingsUtil; +import com.android.voicemail.impl.settings.VoicemailChangePinActivity; +import com.android.voicemail.impl.sms.OmtpMessageSender; +import com.android.voicemail.impl.sms.StatusMessage; +import com.android.voicemail.impl.sms.Vvm3MessageSender; +import com.android.voicemail.impl.sync.VvmNetworkRequest; +import com.android.voicemail.impl.sync.VvmNetworkRequest.NetworkWrapper; +import com.android.voicemail.impl.sync.VvmNetworkRequest.RequestFailedException; +import java.io.IOException; +import java.security.SecureRandom; +import java.util.Locale; + +/** + * A flavor of OMTP protocol with a different provisioning process + * + *

Used by carriers such as Verizon Wireless + */ +@TargetApi(VERSION_CODES.O) +public class Vvm3Protocol extends VisualVoicemailProtocol { + + private static final String TAG = "Vvm3Protocol"; + + private static final String SMS_EVENT_UNRECOGNIZED = "UNRECOGNIZED"; + private static final String SMS_EVENT_UNRECOGNIZED_CMD = "cmd"; + private static final String SMS_EVENT_UNRECOGNIZED_STATUS = "STATUS"; + private static final String DEFAULT_VMG_URL_KEY = "default_vmg_url"; + + private static final String IMAP_CHANGE_TUI_PWD_FORMAT = "CHANGE_TUI_PWD PWD=%1$s OLD_PWD=%2$s"; + private static final String IMAP_CHANGE_VM_LANG_FORMAT = "CHANGE_VM_LANG Lang=%1$s"; + private static final String IMAP_CLOSE_NUT = "CLOSE_NUT"; + + private static final String ISO639_Spanish = "es"; + + /** + * For VVM3, if the STATUS SMS returns {@link StatusMessage#getProvisioningStatus()} of {@link + * OmtpConstants#SUBSCRIBER_UNKNOWN} and {@link StatusMessage#getReturnCode()} of this value, the + * user can self-provision visual voicemail service. For other response codes, the user must + * contact customer support to resolve the issue. + */ + private static final String VVM3_UNKNOWN_SUBSCRIBER_CAN_SUBSCRIBE_RESPONSE_CODE = "2"; + + // Default prompt level when using the telephone user interface. + // Standard prompt when the user call into the voicemail, and no prompts when someone else is + // leaving a voicemail. + private static final String VVM3_VM_LANGUAGE_ENGLISH_STANDARD_NO_GUEST_PROMPTS = "5"; + private static final String VVM3_VM_LANGUAGE_SPANISH_STANDARD_NO_GUEST_PROMPTS = "6"; + + private static final int DEFAULT_PIN_LENGTH = 6; + + @Override + public void startActivation( + OmtpVvmCarrierConfigHelper config, @Nullable PendingIntent sentIntent) { + // VVM3 does not support activation SMS. + // Send a status request which will start the provisioning process if the user is not + // provisioned. + VvmLog.i(TAG, "Activating"); + config.requestStatus(sentIntent); + } + + @Override + public void startDeactivation(OmtpVvmCarrierConfigHelper config) { + // VVM3 does not support deactivation. + // do nothing. + } + + @Override + public boolean supportsProvisioning() { + return true; + } + + @Override + public void startProvisioning( + ActivationTask task, + PhoneAccountHandle phoneAccountHandle, + OmtpVvmCarrierConfigHelper config, + VoicemailStatus.Editor status, + StatusMessage message, + Bundle data) { + VvmLog.i(TAG, "start vvm3 provisioning"); + if (OmtpConstants.SUBSCRIBER_UNKNOWN.equals(message.getProvisioningStatus())) { + VvmLog.i(TAG, "Provisioning status: Unknown"); + if (VVM3_UNKNOWN_SUBSCRIBER_CAN_SUBSCRIBE_RESPONSE_CODE.equals(message.getReturnCode())) { + VvmLog.i(TAG, "Self provisioning available, subscribing"); + new Vvm3Subscriber(task, phoneAccountHandle, config, status, data).subscribe(); + } else { + config.handleEvent(status, OmtpEvents.VVM3_SUBSCRIBER_UNKNOWN); + } + } else if (OmtpConstants.SUBSCRIBER_NEW.equals(message.getProvisioningStatus())) { + VvmLog.i(TAG, "setting up new user"); + // Save the IMAP credentials in preferences so they are persistent and can be retrieved. + VisualVoicemailPreferences prefs = + new VisualVoicemailPreferences(config.getContext(), phoneAccountHandle); + message.putStatus(prefs.edit()).apply(); + + startProvisionNewUser(task, phoneAccountHandle, config, status, message); + } else if (OmtpConstants.SUBSCRIBER_PROVISIONED.equals(message.getProvisioningStatus())) { + VvmLog.i(TAG, "User provisioned but not activated, disabling VVM"); + VisualVoicemailSettingsUtil.setEnabled(config.getContext(), phoneAccountHandle, false); + } else if (OmtpConstants.SUBSCRIBER_BLOCKED.equals(message.getProvisioningStatus())) { + VvmLog.i(TAG, "User blocked"); + config.handleEvent(status, OmtpEvents.VVM3_SUBSCRIBER_BLOCKED); + } + } + + @Override + public OmtpMessageSender createMessageSender( + Context context, + PhoneAccountHandle phoneAccountHandle, + short applicationPort, + String destinationNumber) { + return new Vvm3MessageSender(context, phoneAccountHandle, applicationPort, destinationNumber); + } + + @Override + public void handleEvent( + Context context, + OmtpVvmCarrierConfigHelper config, + VoicemailStatus.Editor status, + OmtpEvents event) { + Vvm3EventHandler.handleEvent(context, config, status, event); + } + + @Override + public String getCommand(String command) { + switch (command) { + case OmtpConstants.IMAP_CHANGE_TUI_PWD_FORMAT: + return IMAP_CHANGE_TUI_PWD_FORMAT; + case OmtpConstants.IMAP_CLOSE_NUT: + return IMAP_CLOSE_NUT; + case OmtpConstants.IMAP_CHANGE_VM_LANG_FORMAT: + return IMAP_CHANGE_VM_LANG_FORMAT; + default: + return super.getCommand(command); + } + } + + @Override + public Bundle translateStatusSmsBundle( + OmtpVvmCarrierConfigHelper config, String event, Bundle data) { + // UNRECOGNIZED?cmd=STATUS is the response of a STATUS request when the user is provisioned + // with iPhone visual voicemail without VoLTE. Translate it into an unprovisioned status + // so provisioning can be done. + if (!SMS_EVENT_UNRECOGNIZED.equals(event)) { + return null; + } + if (!SMS_EVENT_UNRECOGNIZED_STATUS.equals(data.getString(SMS_EVENT_UNRECOGNIZED_CMD))) { + return null; + } + Bundle bundle = new Bundle(); + bundle.putString(OmtpConstants.PROVISIONING_STATUS, OmtpConstants.SUBSCRIBER_UNKNOWN); + bundle.putString( + OmtpConstants.RETURN_CODE, VVM3_UNKNOWN_SUBSCRIBER_CAN_SUBSCRIBE_RESPONSE_CODE); + String vmgUrl = config.getString(DEFAULT_VMG_URL_KEY); + if (TextUtils.isEmpty(vmgUrl)) { + VvmLog.e(TAG, "Unable to translate STATUS SMS: VMG URL is not set in config"); + return null; + } + bundle.putString(Vvm3Subscriber.VMG_URL_KEY, vmgUrl); + VvmLog.i(TAG, "UNRECOGNIZED?cmd=STATUS translated into unprovisioned STATUS SMS"); + return bundle; + } + + private void startProvisionNewUser( + ActivationTask task, + PhoneAccountHandle phoneAccountHandle, + OmtpVvmCarrierConfigHelper config, + VoicemailStatus.Editor status, + StatusMessage message) { + try (NetworkWrapper wrapper = + VvmNetworkRequest.getNetwork(config, phoneAccountHandle, status)) { + Network network = wrapper.get(); + + VvmLog.i(TAG, "new user: network available"); + try (ImapHelper helper = + new ImapHelper(config.getContext(), phoneAccountHandle, network, status)) { + // VVM3 has inconsistent error language code to OMTP. Just issue a raw command + // here. + // TODO(b/29082671): use LocaleList + if (Locale.getDefault().getLanguage().equals(new Locale(ISO639_Spanish).getLanguage())) { + // Spanish + helper.changeVoicemailTuiLanguage(VVM3_VM_LANGUAGE_SPANISH_STANDARD_NO_GUEST_PROMPTS); + } else { + // English + helper.changeVoicemailTuiLanguage(VVM3_VM_LANGUAGE_ENGLISH_STANDARD_NO_GUEST_PROMPTS); + } + VvmLog.i(TAG, "new user: language set"); + + if (setPin(config.getContext(), phoneAccountHandle, helper, message)) { + // Only close new user tutorial if the PIN has been changed. + helper.closeNewUserTutorial(); + VvmLog.i(TAG, "new user: NUT closed"); + + config.requestStatus(null); + } + } catch (InitializingException | MessagingException | IOException e) { + config.handleEvent(status, OmtpEvents.VVM3_NEW_USER_SETUP_FAILED); + task.fail(); + VvmLog.e(TAG, e.toString()); + } + } catch (RequestFailedException e) { + config.handleEvent(status, OmtpEvents.DATA_NO_CONNECTION_CELLULAR_REQUIRED); + task.fail(); + } + } + + private static boolean setPin( + Context context, + PhoneAccountHandle phoneAccountHandle, + ImapHelper helper, + StatusMessage message) + throws IOException, MessagingException { + String defaultPin = getDefaultPin(message); + if (defaultPin == null) { + VvmLog.i(TAG, "cannot generate default PIN"); + return false; + } + + if (VoicemailChangePinActivity.isDefaultOldPinSet(context, phoneAccountHandle)) { + // The pin was already set + VvmLog.i(TAG, "PIN already set"); + return true; + } + String newPin = generatePin(getMinimumPinLength(context, phoneAccountHandle)); + if (helper.changePin(defaultPin, newPin) == OmtpConstants.CHANGE_PIN_SUCCESS) { + VoicemailChangePinActivity.setDefaultOldPIN(context, phoneAccountHandle, newPin); + helper.handleEvent(OmtpEvents.CONFIG_DEFAULT_PIN_REPLACED); + } + VvmLog.i(TAG, "new user: PIN set"); + return true; + } + + @Nullable + private static String getDefaultPin(StatusMessage message) { + // The IMAP username is [phone number]@example.com + String username = message.getImapUserName(); + try { + String number = username.substring(0, username.indexOf('@')); + if (number.length() < 4) { + VvmLog.e(TAG, "unable to extract number from IMAP username"); + return null; + } + return "1" + number.substring(number.length() - 4); + } catch (StringIndexOutOfBoundsException e) { + VvmLog.e(TAG, "unable to extract number from IMAP username"); + return null; + } + } + + private static int getMinimumPinLength(Context context, PhoneAccountHandle phoneAccountHandle) { + VisualVoicemailPreferences preferences = + new VisualVoicemailPreferences(context, phoneAccountHandle); + // The OMTP pin length format is {min}-{max} + String[] lengths = preferences.getString(OmtpConstants.TUI_PASSWORD_LENGTH, "").split("-"); + if (lengths.length == 2) { + try { + return Integer.parseInt(lengths[0]); + } catch (NumberFormatException e) { + return DEFAULT_PIN_LENGTH; + } + } + return DEFAULT_PIN_LENGTH; + } + + private static String generatePin(int length) { + SecureRandom random = new SecureRandom(); + return String.format(Locale.US, "%010d", Math.abs(random.nextLong())).substring(0, length); + } +} diff --git a/java/com/android/voicemail/impl/protocol/Vvm3Subscriber.java b/java/com/android/voicemail/impl/protocol/Vvm3Subscriber.java new file mode 100644 index 000000000..c8a74c8d5 --- /dev/null +++ b/java/com/android/voicemail/impl/protocol/Vvm3Subscriber.java @@ -0,0 +1,334 @@ +/* + * 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.voicemail.impl.protocol; + +import android.annotation.TargetApi; +import android.net.Network; +import android.os.Build; +import android.os.Build.VERSION_CODES; +import android.os.Bundle; +import android.support.annotation.WorkerThread; +import android.telecom.PhoneAccountHandle; +import android.telephony.TelephonyManager; +import android.text.Html; +import android.text.Spanned; +import android.text.style.URLSpan; +import android.util.ArrayMap; +import com.android.voicemail.impl.ActivationTask; +import com.android.voicemail.impl.Assert; +import com.android.voicemail.impl.OmtpEvents; +import com.android.voicemail.impl.OmtpVvmCarrierConfigHelper; +import com.android.voicemail.impl.VoicemailStatus; +import com.android.voicemail.impl.VvmLog; +import com.android.voicemail.impl.sync.VvmNetworkRequest; +import com.android.voicemail.impl.sync.VvmNetworkRequest.NetworkWrapper; +import com.android.voicemail.impl.sync.VvmNetworkRequest.RequestFailedException; +import com.android.volley.AuthFailureError; +import com.android.volley.Request; +import com.android.volley.RequestQueue; +import com.android.volley.toolbox.HurlStack; +import com.android.volley.toolbox.RequestFuture; +import com.android.volley.toolbox.StringRequest; +import com.android.volley.toolbox.Volley; +import java.io.IOException; +import java.net.CookieHandler; +import java.net.CookieManager; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.Locale; +import java.util.Map; +import java.util.Random; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * Class to subscribe to basic VVM3 visual voicemail, for example, Verizon. Subscription is required + * when the user is unprovisioned. This could happen when the user is on a legacy service, or + * switched over from devices that used other type of visual voicemail. + * + *

The STATUS SMS will come with a URL to the voicemail management gateway. From it we can find + * the self provisioning gateway URL that we can modify voicemail services. + * + *

A request to the self provisioning gateway to activate basic visual voicemail will return us + * with a web page. If the user hasn't subscribe to it yet it will contain a link to confirm the + * subscription. This link should be clicked through cellular network, and have cookies enabled. + * + *

After the process is completed, the carrier should send us another STATUS SMS with a new or + * ready user. + */ +@TargetApi(VERSION_CODES.O) +public class Vvm3Subscriber { + + private static final String TAG = "Vvm3Subscriber"; + + private static final String OPERATION_GET_SPG_URL = "retrieveSPGURL"; + private static final String SPG_URL_TAG = "spgurl"; + private static final String TRANSACTION_ID_TAG = "transactionid"; + //language=XML + private static final String VMG_XML_REQUEST_FORMAT = + "" + + "" + + "" + + " " + + " %1$s" + + " " + + " " + + " %2$s" + + " %3$s" + + " Device" + + " %4$s" + + " " + + ""; + + static final String VMG_URL_KEY = "vmg_url"; + + // Self provisioning POST key/values. VVM3 API 2.1.0 12.3 + private static final String SPG_VZW_MDN_PARAM = "VZW_MDN"; + private static final String SPG_VZW_SERVICE_PARAM = "VZW_SERVICE"; + private static final String SPG_VZW_SERVICE_BASIC = "BVVM"; + private static final String SPG_DEVICE_MODEL_PARAM = "DEVICE_MODEL"; + // Value for all android device + private static final String SPG_DEVICE_MODEL_ANDROID = "DROID_4G"; + private static final String SPG_APP_TOKEN_PARAM = "APP_TOKEN"; + private static final String SPG_APP_TOKEN = "q8e3t5u2o1"; + private static final String SPG_LANGUAGE_PARAM = "SPG_LANGUAGE_PARAM"; + private static final String SPG_LANGUAGE_EN = "ENGLISH"; + + private static final String BASIC_SUBSCRIBE_LINK_TEXT = "Subscribe to Basic Visual Voice Mail"; + + private static final int REQUEST_TIMEOUT_SECONDS = 30; + + private final ActivationTask mTask; + private final PhoneAccountHandle mHandle; + private final OmtpVvmCarrierConfigHelper mHelper; + private final VoicemailStatus.Editor mStatus; + private final Bundle mData; + + private final String mNumber; + + private RequestQueue mRequestQueue; + + private static class ProvisioningException extends Exception { + + public ProvisioningException(String message) { + super(message); + } + } + + static { + // Set the default cookie handler to retain session data for the self provisioning gateway. + // Note; this is not ideal as it is application-wide, and can easily get clobbered. + // But it seems to be the preferred way to manage cookie for HttpURLConnection, and manually + // managing cookies will greatly increase complexity. + CookieManager cookieManager = new CookieManager(); + CookieHandler.setDefault(cookieManager); + } + + @WorkerThread + public Vvm3Subscriber( + ActivationTask task, + PhoneAccountHandle handle, + OmtpVvmCarrierConfigHelper helper, + VoicemailStatus.Editor status, + Bundle data) { + Assert.isNotMainThread(); + mTask = task; + mHandle = handle; + mHelper = helper; + mStatus = status; + mData = data; + + // Assuming getLine1Number() will work with VVM3. For unprovisioned users the IMAP username + // is not included in the status SMS, thus no other way to get the current phone number. + mNumber = + mHelper + .getContext() + .getSystemService(TelephonyManager.class) + .createForPhoneAccountHandle(mHandle) + .getLine1Number(); + } + + @WorkerThread + public void subscribe() { + Assert.isNotMainThread(); + // Cellular data is required to subscribe. + // processSubscription() is called after network is available. + VvmLog.i(TAG, "Subscribing"); + + try (NetworkWrapper wrapper = VvmNetworkRequest.getNetwork(mHelper, mHandle, mStatus)) { + Network network = wrapper.get(); + VvmLog.d(TAG, "provisioning: network available"); + mRequestQueue = + Volley.newRequestQueue(mHelper.getContext(), new NetworkSpecifiedHurlStack(network)); + processSubscription(); + } catch (RequestFailedException e) { + mHelper.handleEvent(mStatus, OmtpEvents.VVM3_VMG_CONNECTION_FAILED); + mTask.fail(); + } + } + + private void processSubscription() { + try { + String gatewayUrl = getSelfProvisioningGateway(); + String selfProvisionResponse = getSelfProvisionResponse(gatewayUrl); + String subscribeLink = findSubscribeLink(selfProvisionResponse); + clickSubscribeLink(subscribeLink); + } catch (ProvisioningException e) { + VvmLog.e(TAG, e.toString()); + mTask.fail(); + } + } + + /** Get the URL to perform self-provisioning from the voicemail management gateway. */ + private String getSelfProvisioningGateway() throws ProvisioningException { + VvmLog.i(TAG, "retrieving SPG URL"); + String response = vvm3XmlRequest(OPERATION_GET_SPG_URL); + return extractText(response, SPG_URL_TAG); + } + + /** + * Sent a request to the self-provisioning gateway, which will return us with a webpage. The page + * might contain a "Subscribe to Basic Visual Voice Mail" link to complete the subscription. The + * cookie from this response and cellular data is required to click the link. + */ + private String getSelfProvisionResponse(String url) throws ProvisioningException { + VvmLog.i(TAG, "Retrieving self provisioning response"); + + RequestFuture future = RequestFuture.newFuture(); + + StringRequest stringRequest = + new StringRequest(Request.Method.POST, url, future, future) { + @Override + protected Map getParams() { + Map params = new ArrayMap<>(); + params.put(SPG_VZW_MDN_PARAM, mNumber); + params.put(SPG_VZW_SERVICE_PARAM, SPG_VZW_SERVICE_BASIC); + params.put(SPG_DEVICE_MODEL_PARAM, SPG_DEVICE_MODEL_ANDROID); + params.put(SPG_APP_TOKEN_PARAM, SPG_APP_TOKEN); + // Language to display the subscription page. The page is never shown to the user + // so just use English. + params.put(SPG_LANGUAGE_PARAM, SPG_LANGUAGE_EN); + return params; + } + }; + + mRequestQueue.add(stringRequest); + try { + return future.get(REQUEST_TIMEOUT_SECONDS, TimeUnit.SECONDS); + } catch (InterruptedException | ExecutionException | TimeoutException e) { + mHelper.handleEvent(mStatus, OmtpEvents.VVM3_SPG_CONNECTION_FAILED); + throw new ProvisioningException(e.toString()); + } + } + + private void clickSubscribeLink(String subscribeLink) throws ProvisioningException { + VvmLog.i(TAG, "Clicking subscribe link"); + RequestFuture future = RequestFuture.newFuture(); + + StringRequest stringRequest = + new StringRequest(Request.Method.POST, subscribeLink, future, future); + mRequestQueue.add(stringRequest); + try { + // A new STATUS SMS will be sent after this request. + future.get(REQUEST_TIMEOUT_SECONDS, TimeUnit.SECONDS); + } catch (TimeoutException | ExecutionException | InterruptedException e) { + mHelper.handleEvent(mStatus, OmtpEvents.VVM3_SPG_CONNECTION_FAILED); + throw new ProvisioningException(e.toString()); + } + // It could take very long for the STATUS SMS to return. Waiting for it is unreliable. + // Just leave the CONFIG STATUS as CONFIGURING and end the task. The user can always + // manually retry if it took too long. + } + + private String vvm3XmlRequest(String operation) throws ProvisioningException { + VvmLog.d(TAG, "Sending vvm3XmlRequest for " + operation); + String voicemailManagementGateway = mData.getString(VMG_URL_KEY); + if (voicemailManagementGateway == null) { + VvmLog.e(TAG, "voicemailManagementGateway url unknown"); + return null; + } + String transactionId = createTransactionId(); + String body = + String.format( + Locale.US, VMG_XML_REQUEST_FORMAT, transactionId, mNumber, operation, Build.MODEL); + + RequestFuture future = RequestFuture.newFuture(); + StringRequest stringRequest = + new StringRequest(Request.Method.POST, voicemailManagementGateway, future, future) { + @Override + public byte[] getBody() throws AuthFailureError { + return body.getBytes(); + } + }; + mRequestQueue.add(stringRequest); + + try { + String response = future.get(REQUEST_TIMEOUT_SECONDS, TimeUnit.SECONDS); + if (!transactionId.equals(extractText(response, TRANSACTION_ID_TAG))) { + throw new ProvisioningException("transactionId mismatch"); + } + return response; + } catch (InterruptedException | ExecutionException | TimeoutException e) { + mHelper.handleEvent(mStatus, OmtpEvents.VVM3_VMG_CONNECTION_FAILED); + throw new ProvisioningException(e.toString()); + } + } + + private String findSubscribeLink(String response) throws ProvisioningException { + Spanned doc = Html.fromHtml(response, Html.FROM_HTML_MODE_LEGACY); + URLSpan[] spans = doc.getSpans(0, doc.length(), URLSpan.class); + StringBuilder fulltext = new StringBuilder(); + for (URLSpan span : spans) { + String text = doc.subSequence(doc.getSpanStart(span), doc.getSpanEnd(span)).toString(); + if (BASIC_SUBSCRIBE_LINK_TEXT.equals(text)) { + return span.getURL(); + } + fulltext.append(text); + } + throw new ProvisioningException("Subscribe link not found: " + fulltext); + } + + private String createTransactionId() { + return String.valueOf(Math.abs(new Random().nextLong())); + } + + private String extractText(String xml, String tag) throws ProvisioningException { + Pattern pattern = Pattern.compile("<" + tag + ">(.*)<\\/" + tag + ">"); + Matcher matcher = pattern.matcher(xml); + if (matcher.find()) { + return matcher.group(1); + } + throw new ProvisioningException("Tag " + tag + " not found in xml response"); + } + + private static class NetworkSpecifiedHurlStack extends HurlStack { + + private final Network mNetwork; + + public NetworkSpecifiedHurlStack(Network network) { + mNetwork = network; + } + + @Override + protected HttpURLConnection createConnection(URL url) throws IOException { + return (HttpURLConnection) mNetwork.openConnection(url); + } + } +} diff --git a/java/com/android/voicemail/impl/res/layout/voicemail_change_pin.xml b/java/com/android/voicemail/impl/res/layout/voicemail_change_pin.xml new file mode 100644 index 000000000..50c92777e --- /dev/null +++ b/java/com/android/voicemail/impl/res/layout/voicemail_change_pin.xml @@ -0,0 +1,97 @@ + + + + + + + + + + + + + + + + + + + + + +