• 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 #include "ohos/aafwk/content/intent_filter.h"
17 
18 #include "string_ex.h"
19 #include "ohos/aafwk/content/intent.h"
20 
21 namespace OHOS {
22 namespace AAFwk {
23 
IntentFilter()24 IntentFilter::IntentFilter()
25 {}
26 
GetEntity() const27 std::string IntentFilter::GetEntity() const
28 {
29     return entity_;
30 }
31 
SetEntity(const std::string & entity)32 void IntentFilter::SetEntity(const std::string &entity)
33 {
34     entity_ = entity;
35 }
36 
AddAction(const std::string & action)37 void IntentFilter::AddAction(const std::string &action)
38 {
39     auto it = std::find(actions_.cbegin(), actions_.cend(), action);
40     if (it == actions_.cend()) {
41         actions_.push_back(action);
42     }
43 }
44 
CountAction() const45 int IntentFilter::CountAction() const
46 {
47     return actions_.size();
48 }
49 
GetAction(int index) const50 std::string IntentFilter::GetAction(int index) const
51 {
52     std::string action;
53     if (index < static_cast<int>(actions_.size())) {
54         action = actions_[index];
55     }
56     return action;
57 }
58 
RemoveAction(const std::string & action)59 void IntentFilter::RemoveAction(const std::string &action)
60 {
61     auto it = std::find(actions_.cbegin(), actions_.cend(), action);
62     if (it != actions_.cend()) {
63         actions_.erase(it);
64     }
65 }
66 
HasAction(const std::string & action) const67 bool IntentFilter::HasAction(const std::string &action) const
68 {
69     return std::find(actions_.cbegin(), actions_.cend(), action) != actions_.cend();
70 }
71 
Marshalling(Parcel & parcel) const72 bool IntentFilter::Marshalling(Parcel &parcel) const
73 {
74     // write entity
75     if (!parcel.WriteString16(Str8ToStr16(entity_))) {
76         return false;
77     }
78 
79     // write actions
80     std::vector<std::u16string> actionU16;
81     for (std::vector<std::string>::size_type i = 0; i < actions_.size(); i++) {
82         actionU16.push_back(Str8ToStr16(actions_[i]));
83     }
84 
85     return parcel.WriteString16Vector(actionU16);
86 }
87 
ReadFromParcel(Parcel & parcel)88 bool IntentFilter::ReadFromParcel(Parcel &parcel)
89 {
90     // read entity
91     entity_ = Str16ToStr8(parcel.ReadString16());
92 
93     // read actions
94     std::vector<std::u16string> actionU16;
95     if (!parcel.ReadString16Vector(&actionU16)) {
96         return false;
97     }
98 
99     actions_.clear();
100     for (std::vector<std::u16string>::size_type i = 0; i < actionU16.size(); i++) {
101         actions_.push_back(Str16ToStr8(actionU16[i]));
102     }
103 
104     return true;
105 }
106 
Unmarshalling(Parcel & parcel)107 IntentFilter *IntentFilter::Unmarshalling(Parcel &parcel)
108 {
109     IntentFilter *filter = new (std::nothrow) IntentFilter();
110     if (filter && !filter->ReadFromParcel(parcel)) {
111         delete filter;
112         filter = nullptr;
113     }
114     return filter;
115 }
116 
MatchAction(const std::string & action) const117 bool IntentFilter::MatchAction(const std::string &action) const
118 {
119     return HasAction(action);
120 }
121 
MatchEntity(const std::string & entity) const122 bool IntentFilter::MatchEntity(const std::string &entity) const
123 {
124     return entity_ == entity;
125 }
126 
Match(const Intent & intent) const127 bool IntentFilter::Match(const Intent &intent) const
128 {
129     return MatchAction(intent.GetAction()) && MatchEntity(intent.GetEntity());
130 }
131 
132 }  // namespace AAFwk
133 }  // namespace OHOS
134