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 DATA_STORAGE_PDP_PROFILE_ABILITY_H
17 #define DATA_STORAGE_PDP_PROFILE_ABILITY_H
18 
19 #include <functional>
20 
21 #include "__mutex_base"
22 #include "ability.h"
23 #include "ability_lifecycle.h"
24 #include "abs_shared_result_set.h"
25 #include "datashare_ext_ability.h"
26 #include "datashare_ext_ability_context.h"
27 #include "datashare_values_bucket.h"
28 #include "iosfwd"
29 #include "map"
30 #include "memory"
31 #include "rdb_pdp_profile_helper.h"
32 #include "rdb_predicates.h"
33 #include "string"
34 #include "vector"
35 #include "want.h"
36 
37 namespace OHOS {
38 class Uri;
39 namespace NativeRdb {
40 class AbsRdbPredicates;
41 class ResultSet;
42 class DataAbilityPredicates;
43 class ValuesBucket;
44 }
45 namespace Telephony {
46 enum class PdpProfileUriType {
47     UNKNOW,
48     PDP_PROFILE,
49     INIT,
50     RESET,
51     PREFER_APN,
52 };
53 class PdpProfileAbility : public DataShare::DataShareExtAbility {
54 public:
55     PdpProfileAbility();
56     virtual ~PdpProfileAbility() override;
57     static PdpProfileAbility* Create();
58     void DoInit();
59     sptr<IRemoteObject> OnConnect(const AAFwk::Want &want) override;
60     virtual void OnStart(const AppExecFwk::Want &want) override;
61     virtual int Insert(const Uri &uri, const DataShare::DataShareValuesBucket &value) override;
62     virtual int BatchInsert(const Uri &uri, const std::vector<DataShare::DataShareValuesBucket> &values) override;
63     virtual std::shared_ptr<DataShare::DataShareResultSet> Query(const Uri &uri,
64         const DataShare::DataSharePredicates &predicates, std::vector<std::string> &columns,
65         DataShare::DatashareBusinessError &businessError) override;
66     virtual int Update(const Uri &uri, const DataShare::DataSharePredicates &predicates,
67         const DataShare::DataShareValuesBucket &value) override;
68     virtual int Delete(const Uri &uri, const DataShare::DataSharePredicates &predicates) override;
69     virtual std::string GetType(const Uri &uri) override;
70     virtual int OpenFile(const Uri &uri, const std::string &mode) override;
71 
72 private:
73     /**
74      * Parse Uri Type
75      *
76      * @param uri Resource address
77      * @return return PdpProfileUriType
78      */
79     PdpProfileUriType ParseUriType(Uri &uri);
80 
81     /**
82      * Convert DataSharePredicates to RdbPredicates
83      *
84      * @param tableName table name of the predicates
85      * @param predicates DataSharePredicates
86      */
87     OHOS::NativeRdb::RdbPredicates ConvertPredicates(
88         const std::string &tableName, const DataShare::DataSharePredicates &predicates);
89 
90     /**
91      * Check whether the initialization succeeds
92      *
93      * @return true : succeed ,false : failed
94      */
95     bool IsInitOk();
96     int UpdatePreferApn(const DataShare::DataShareValuesBucket &value);
97     int SetPreferApn(int simId, int profileId);
98     int GetPreferApn(const std::string &queryString);
99     std::string GetQueryKey(const std::string &queryString, const std::string &key);
100     bool HasColumnValue(
101         const OHOS::NativeRdb::ValuesBucket &value, const char *columnName, NativeRdb::ValueObject &valueObject);
102 
103 private:
104     RdbPdpProfileHelper helper_;
105     std::mutex lock_;
106     bool initDatabaseDir = false;
107     bool initRdbStore = false;
108     void GetTargetOpkey(int slotId, std::string &opkey);
109     int ResetApn(const DataShare::DataShareValuesBucket &bucket);
110     int GetIntFromValuesBucket(OHOS::NativeRdb::ValuesBucket &bucket, const char *key, int &value);
111     std::shared_ptr<NativeRdb::ResultSet> QueryPdpProfile(Uri &uri, const std::string &tableName,
112         const DataShare::DataSharePredicates &predicates, std::vector<std::string> &columns);
113 };
114 } // namespace Telephony
115 } // namespace OHOS
116 #endif // DATA_STORAGE_PDP_PROFILE_ABILITY_H
117