1 /*
2  * Copyright (C) 2024 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 "ringtone_source.h"
17 
18 #include "ringtone_db_const.h"
19 
20 using namespace std;
21 
22 namespace OHOS {
23 namespace Media {
24 
25 const string RingtoneRestoreRdbOpenCb::CREATE_RINGTONE_TABLE = "CREATE TABLE IF NOT EXISTS " + RINGTONE_TABLE + "(" +
26     RINGTONE_COLUMN_TONE_ID                       + " INTEGER  PRIMARY KEY AUTOINCREMENT, " +
27     RINGTONE_COLUMN_DATA                          + " TEXT              , " +
28     RINGTONE_COLUMN_SIZE                          + " BIGINT   DEFAULT 0, " +
29     RINGTONE_COLUMN_DISPLAY_NAME                  + " TEXT              , " +
30     RINGTONE_COLUMN_TITLE                         + " TEXT              , " +
31     RINGTONE_COLUMN_MEDIA_TYPE                    + " INT      DEFAULT 0, " +
32     RINGTONE_COLUMN_TONE_TYPE                     + " INT      DEFAULT 0, " +
33     RINGTONE_COLUMN_MIME_TYPE                     + " TEXT              , " +
34     RINGTONE_COLUMN_SOURCE_TYPE                   + " INT      DEFAULT 0, " +
35     RINGTONE_COLUMN_DATE_ADDED                    + " BIGINT   DEFAULT 0, " +
36     RINGTONE_COLUMN_DATE_MODIFIED                 + " BIGINT   DEFAULT 0, " +
37     RINGTONE_COLUMN_DATE_TAKEN                    + " BIGINT   DEFAULT 0, " +
38     RINGTONE_COLUMN_DURATION                      + " INT      DEFAULT 0, " +
39     RINGTONE_COLUMN_SHOT_TONE_TYPE                + " INT      DEFAULT 0, " +
40     RINGTONE_COLUMN_SHOT_TONE_SOURCE_TYPE         + " INT      DEFAULT 0, " +
41     RINGTONE_COLUMN_NOTIFICATION_TONE_TYPE        + " INT      DEFAULT 0, " +
42     RINGTONE_COLUMN_NOTIFICATION_TONE_SOURCE_TYPE + " INT      DEFAULT 0, " +
43     RINGTONE_COLUMN_RING_TONE_TYPE                + " INT      DEFAULT 0, " +
44     RINGTONE_COLUMN_RING_TONE_SOURCE_TYPE         + " INT      DEFAULT 0, " +
45     RINGTONE_COLUMN_ALARM_TONE_TYPE               + " INT      DEFAULT 0, " +
46     RINGTONE_COLUMN_ALARM_TONE_SOURCE_TYPE        + " INT      DEFAULT 0  " + ")";
47 
OnCreate(NativeRdb::RdbStore & store)48 int RingtoneRestoreRdbOpenCb::OnCreate(NativeRdb::RdbStore &store)
49 {
50     return store.ExecuteSql(CREATE_RINGTONE_TABLE);
51 }
52 
OnUpgrade(NativeRdb::RdbStore & store,int oldVersion,int newVersion)53 int RingtoneRestoreRdbOpenCb::OnUpgrade(NativeRdb::RdbStore &store, int oldVersion, int newVersion)
54 {
55     return 0;
56 }
57 
OnCreate(NativeRdb::RdbStore & store)58 int RingtoneLocalRdbOpenCb::OnCreate(NativeRdb::RdbStore &store)
59 {
60     return 0;
61 }
62 
OnUpgrade(NativeRdb::RdbStore & store,int oldVersion,int newVersion)63 int RingtoneLocalRdbOpenCb::OnUpgrade(NativeRdb::RdbStore &store, int oldVersion, int newVersion)
64 {
65     return 0;
66 }
67 
Init(const std::string & restoreDbPath,const std::string & localDbPath)68 void RingtoneSource::Init(const std::string &restoreDbPath, const std::string &localDbPath)
69 {
70     int errCode = 0;
71     NativeRdb::RdbStoreConfig localRdbConfig(localDbPath);
72     RingtoneLocalRdbOpenCb localDbHelper;
73     localRdbPtr_ = NativeRdb::RdbHelper::GetRdbStore(localRdbConfig, 1, localDbHelper, errCode);
74     NativeRdb::RdbStoreConfig restoreRdbConfig(restoreDbPath);
75     RingtoneRestoreRdbOpenCb restoreDbHelper;
76     restoreRdbPtr_ = NativeRdb::RdbHelper::GetRdbStore(restoreRdbConfig, 1, restoreDbHelper, errCode);
77     InitRingtoneDb();
78 }
79 
InitRingtoneDb()80 void RingtoneSource::InitRingtoneDb()
81 {
82     restoreRdbPtr_->ExecuteSql("INSERT INTO " + RINGTONE_TABLE +
83         " VALUES (last_insert_rowid()+1, '/storage/media/local/files/Ringtone/alarms/Adara.ogg'," +
84         " 10414, 'Adara.ogg', 'Adara', 2, 0, 'audio/ogg', 2, 1505707241000, 1505707241846, 1505707241," +
85         " 600, 0, -1, 0, -1, 0, -1, 1, 2)");
86     restoreRdbPtr_->ExecuteSql("INSERT INTO " + RINGTONE_TABLE +
87         " VALUES (last_insert_rowid()+1, '/storage/media/local/files/Ringtone/ringtones/Carme.ogg'," +
88         " 26177, 'Carme.ogg', 'Carme', 2, 1, 'audio/ogg', 2, 1505707241000, 1505707241846, 1505707241," +
89         " 1242, 0, -1, 0, -1, 3, 2, 0, -1)");
90     restoreRdbPtr_->ExecuteSql("INSERT INTO " + RINGTONE_TABLE +
91         " VALUES (last_insert_rowid()+1, '/storage/media/local/files/Ringtone/notifications/Radon.ogg'," +
92         " 25356, 'Radon.ogg', 'Radon', 2, 2, 'audio/ogg', 2, 1505707241000, 1505707241846, 1505707241," +
93         " 1800, 0, -1, 1, 2, 0, -1, 0, -1)");
94     restoreRdbPtr_->ExecuteSql("INSERT INTO " + RINGTONE_TABLE +
95         " VALUES (last_insert_rowid()+1, '/storage/media/local/files/Ringtone/notifications/Titan.ogg'," +
96         " 30984, 'Titan.ogg', 'Titan', 2, 2, 'audio/ogg', 2, 1505707241000, 1505707241846, 1505707241," +
97         " 1947, 3, 2, 0, -1, 0, -1, 0, -1)");
98 }
99 } // namespace Media
100 } // namespace OHOS