• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "ring.h"
17 
18 #include <thread>
19 
20 #include "telephony_log_wrapper.h"
21 
22 #include "audio_player.h"
23 
24 namespace OHOS {
25 namespace Telephony {
Ring()26 Ring::Ring() : isVibrating_(false), shouldRing_(false), shouldVibrate_(false), ringtonePath_("")
27 {
28     Init(DelayedSingleton<AudioProxy>::GetInstance()->GetDefaultRingPath());
29 }
30 
Ring(const std::string & path)31 Ring::Ring(const std::string &path)
32     : isVibrating_(false), shouldRing_(false), shouldVibrate_(false), ringtonePath_("")
33 {
34     Init(path);
35 }
36 
~Ring()37 Ring::~Ring() {}
38 
Init(const std::string & ringtonePath)39 void Ring::Init(const std::string &ringtonePath)
40 {
41     if (ringtonePath.empty()) {
42         TELEPHONY_LOGE("ringtone path empty");
43         return;
44     }
45     if (AudioStandard::AudioRingerMode::RINGER_MODE_NORMAL ==
46         DelayedSingleton<AudioProxy>::GetInstance()->GetRingerMode()) {
47         shouldRing_ = true;
48         shouldVibrate_ = true;
49     } else if (AudioStandard::AudioRingerMode::RINGER_MODE_VIBRATE ==
50         DelayedSingleton<AudioProxy>::GetInstance()->GetRingerMode()) {
51         shouldRing_ = false;
52         shouldVibrate_ = true;
53     } else if (AudioStandard::AudioRingerMode::RINGER_MODE_SILENT ==
54         DelayedSingleton<AudioProxy>::GetInstance()->GetRingerMode()) {
55         shouldRing_ = false;
56         shouldVibrate_ = false;
57     }
58     ringtonePath_ = ringtonePath;
59 }
60 
Play()61 int32_t Ring::Play()
62 {
63     if (!shouldRing_ || ringtonePath_.empty()) {
64         TELEPHONY_LOGE("should not ring or ringtone path empty");
65         return CALL_ERR_INVALID_PATH;
66     }
67     int32_t result = TELEPHONY_SUCCESS;
68     std::thread play(
69         AudioPlayer::Play, ringtonePath_, AudioStandard::AudioStreamType::STREAM_RING, PlayerType::TYPE_RING);
70     play.detach();
71     if (shouldVibrate_) {
72         result = StartVibrate();
73     }
74     return result;
75 }
76 
Stop()77 int32_t Ring::Stop()
78 {
79     std::lock_guard<std::mutex> lock(mutex_);
80     if (!shouldRing_ || ringtonePath_.empty()) {
81         TELEPHONY_LOGE("should not ring or ringtone path empty");
82         return CALL_ERR_INVALID_PATH;
83     }
84     int32_t result = TELEPHONY_SUCCESS;
85     AudioPlayer::SetStop(PlayerType::TYPE_RING, true);
86     if (isVibrating_) {
87         result = CancelVibrate();
88     }
89     return result;
90 }
91 
StartVibrate()92 int32_t Ring::StartVibrate()
93 {
94     if (DelayedSingleton<AudioProxy>::GetInstance()->StartVibrate() == TELEPHONY_SUCCESS) {
95         isVibrating_ = true;
96         return TELEPHONY_SUCCESS;
97     }
98     TELEPHONY_LOGE("start vibrate failed");
99     return CALL_ERR_AUDIO_START_VIBRATE_FAILED;
100 }
101 
CancelVibrate()102 int32_t Ring::CancelVibrate()
103 {
104     if (DelayedSingleton<AudioProxy>::GetInstance()->CancelVibrate() == TELEPHONY_SUCCESS) {
105         isVibrating_ = false;
106         return TELEPHONY_SUCCESS;
107     }
108     TELEPHONY_LOGE("cancel vibrate failed");
109     return CALL_ERR_AUDIO_CANCEL_VIBRATE_FAILED;
110 }
111 
ShouldVibrate()112 bool Ring::ShouldVibrate()
113 {
114     return DelayedSingleton<AudioProxy>::GetInstance()->GetRingerMode() !=
115         AudioStandard::AudioRingerMode::RINGER_MODE_SILENT;
116 }
117 } // namespace Telephony
118 } // namespace OHOS