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