• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 #ifndef AUDIO_RINGTONE_MANAGER_H
17 #define AUDIO_RINGTONE_MANAGER_H
18 
19 #include <array>
20 
21 #include "iringtone_sound_manager.h"
22 #include "audio_log.h"
23 #include "audio_errors.h"
24 #include "player.h"
25 #include "foundation/ability/ability_runtime/interfaces/kits/native/appkit/ability_runtime/context/context.h"
26 #include "abs_shared_result_set.h"
27 #include "data_ability_helper.h"
28 #include "data_ability_predicates.h"
29 #include "rdb_errno.h"
30 #include "result_set.h"
31 #include "uri.h"
32 #include "values_bucket.h"
33 #include "want.h"
34 
35 namespace OHOS {
36 namespace AudioStandard {
37 namespace {
38 const int32_t SIM_COUNT = 2;
39 const int32_t OPERATION_TYPE = 2;
40 const int32_t URI_TYPE = 3;
41 const int32_t SET_URI_INDEX = 0;
42 const int32_t GET_URI_INDEX = 1;
43 const int32_t RINGTONE_INDEX = 0;
44 const int32_t NOTIFICATION_INDEX = 1;
45 const int32_t ALARM_INDEX = 2;
46 const float HIGH_VOL = 1.0f;
47 const float LOW_VOL = 0.0f;
48 const std::string MEDIA_KVSTOREOPRN = "kvstore_operation";
49 const std::string MEDIA_DATA_DB_RINGTONE_TYPE = "ringtone_type";
50 const std::string MEDIA_DATA_DB_ALARM_URI = "alarm_uri";
51 const std::string MEDIA_DATA_DB_RINGTONE_URI = "ringtone_uri";
52 const std::string MEDIA_DATA_DB_NOTIFICATION_URI = "notification_uri";
53 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "RingtoneSoundManager"};
54 
55 std::string kvstoreOperation[OPERATION_TYPE][URI_TYPE] = {
56     {"set_ringtone_uri", "set_notification_uri", "set_alarm_uri"},
57     {"get_ringtone_uri", "get_notification_uri", "get_alarm_uri"},
58 };
59 }
60 
61 class RingtoneSoundManager : public IRingtoneSoundManager {
62 public:
63     RingtoneSoundManager() = default;
64     ~RingtoneSoundManager();
65 
66     // IRingtoneSoundManager override
67     std::shared_ptr<IRingtonePlayer> GetRingtonePlayer(const std::shared_ptr<AbilityRuntime::Context> &ctx,
68         RingtoneType type) override;
69     int32_t SetSystemNotificationUri(const std::shared_ptr<AbilityRuntime::Context> &ctx,
70         const std::string &uri) override;
71     int32_t SetSystemAlarmUri(const std::shared_ptr<AbilityRuntime::Context> &ctx, const std::string &uri) override;
72     int32_t SetSystemRingtoneUri(const std::shared_ptr<AbilityRuntime::Context> &ctx, const std::string &uri,
73         RingtoneType type) override;
74     std::string GetSystemRingtoneUri(const std::shared_ptr<AbilityRuntime::Context> &ctx, RingtoneType type) override;
75     std::string GetSystemNotificationUri(const std::shared_ptr<AbilityRuntime::Context> &ctx) override;
76     std::string GetSystemAlarmUri(const std::shared_ptr<AbilityRuntime::Context> &ctx) override;
77 
78 private:
79     void CreateDataAbilityHelper(const std::shared_ptr<AbilityRuntime::Context> &ctx);
80     std::string FetchUri(const std::shared_ptr<AbilityRuntime::Context> &context, const std::string &operation);
81     int32_t SetUri(const std::shared_ptr<AbilityRuntime::Context> &context, const NativeRdb::ValuesBucket &valueBucket,
82         const std::string &operation);
83 
84     std::array<std::string, SIM_COUNT> ringtoneUri_ = {};
85     std::array<std::shared_ptr<IRingtonePlayer>, SIM_COUNT> ringtonePlayer_ = {nullptr};
86     std::shared_ptr<AppExecFwk::DataAbilityHelper> abilityHelper_ = nullptr;
87 };
88 
89 class RingtonePlayerCallback;
90 class RingtonePlayer : public IRingtonePlayer {
91 public:
92     RingtonePlayer(const std::shared_ptr<AbilityRuntime::Context> &ctx, RingtoneSoundManager &audioMgr,
93         RingtoneType type);
94     ~RingtonePlayer();
95     void SetPlayerState(RingtoneState ringtoneState);
96 
97     // IRingtone override
98     RingtoneState GetRingtoneState() override;
99     int32_t Configure(const float &volume, const bool &loop) override;
100     int32_t Start() override;
101     int32_t Stop() override;
102     int32_t Release() override;
103     int32_t GetAudioRendererInfo(AudioStandard::AudioRendererInfo &rendererInfo) const override;
104     std::string GetTitle() override;
105 
106 private:
107     void InitialisePlayer();
108     int32_t PrepareRingtonePlayer(bool isReInitNeeded);
109 
110     float volume_ = HIGH_VOL;
111     bool loop_ = false;
112     bool isStartQueued_ = false;
113     std::string configuredUri_ = "";
114     std::shared_ptr<Media::Player> player_ = nullptr;
115     std::shared_ptr<AbilityRuntime::Context> context_;
116     std::shared_ptr<RingtonePlayerCallback> callback_ = nullptr;
117     RingtoneSoundManager &audioRingtoneMgr_;
118     RingtoneType type_ = RINGTONE_TYPE_DEFAULT;
119     RingtoneState ringtoneState_ = STATE_NEW;
120 };
121 
122 class RingtonePlayerCallback : public Media::PlayerCallback {
123 public:
124     explicit RingtonePlayerCallback(RingtonePlayer &ringtonePlayer);
125     virtual ~RingtonePlayerCallback() = default;
126     void OnError(Media::PlayerErrorType errorType, int32_t errorCode) override;
127     void OnInfo(Media::PlayerOnInfoType type, int32_t extra, const Media::Format &infoBody) override;
128 
129 private:
130     Media::PlayerStates state_ = Media::PLAYER_IDLE;
131     RingtoneState ringtoneState_ = STATE_NEW;
132     RingtonePlayer &ringtonePlayer_;
133 };
134 } // namespace AudioStandard
135 } // namespace OHOS
136 #endif // AUDIO_RINGTONE_MANAGER_H
137