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 BASE_ACCOUNT_DEVICE_ACCOUNT_INFO_H
17  #define BASE_ACCOUNT_DEVICE_ACCOUNT_INFO_H
18  
19  #include "string_ex.h"
20  #include "message_parcel.h"
21  
22  namespace OHOS {
23  namespace AccountSA {
24  /**
25   * Device account type.
26   */
27  enum class DeviceAccountType {
28      DEVICE_ACCOUNT_TYPE_INVALID = -1, // invalid type
29      DEVICE_ACCOUNT_TYPE_OWNER = 0, // device owner account
30      DEVICE_ACCOUNT_TYPE_NORMAL, // device normal account
31      DEVICE_ACCOUNT_TYPE_HIDDEN_SPACE, // device hidden space account
32      DEVICE_ACCOUNT_TYPE_REPAIR_MODE, // device repair account
33      DEVICE_ACCOUNT_TYPE_GUEST, // device guest account
34  };
35  
36  /**
37   * Device account state.
38   */
39  enum class DeviceAccountState {
40      DEVICE_ACCOUNT_STATE_INVALID = -1, // invalid state
41      DEVICE_ACCOUNT_STATE_BOOTING = 0, // device account is booting
42      DEVICE_ACCOUNT_STATE_RUNNING_LOCKED, // device account is locked
43      DEVICE_ACCOUNT_STATE_RUNNING_UNLOCKING, // device account is unlocking
44      DEVICE_ACCOUNT_STATE_RUNNING_UNLOCKED, // device account is unlocked
45      DEVICE_ACCOUNT_STATE_STOPPING, // device account is stopping
46      DEVICE_ACCOUNT_STATE_SHUTDOWN, // device account is shutdown
47  };
48  
49  /**
50   * Invalid device account id.
51   */
52  constexpr std::int32_t DEVICE_ACCOUNT_ID_INVALID = -1;
53  
54  /**
55   * Device account owner
56   */
57  constexpr std::int32_t DEVICE_ACCOUNT_OWNER = 0;
58  
59  /**
60   * no error
61   */
62  constexpr std::int32_t ERROR_NONE = 0;
63  
64  /**
65   * error
66   */
67  constexpr std::int32_t ERROR_HAPPEN = -1;
68  
69  class DeviceAccountInfo {
70  public:
71      /**
72       * Device account ID.
73       */
74      std::int32_t id_;
75  
76      /**
77       * Device account type.
78       */
79      DeviceAccountType type_;
80  
81      /**
82       * Device account name.
83       */
84      std::string name_;
85  
86      /**
87       * Device account icon path.
88       */
89      std::string iconPath_;
90  
91      /**
92       * Device account state.
93       */
94      DeviceAccountState state_;
95  
96      /**
97       * Device account flags, like admin or system flag.
98       */
99      std::int32_t flags_;
100  
101      /**
102       * Device account create time.
103       */
104      int64_t creationTime_;
105  
106      /**
107       * Device account last logged time.
108       */
109      int64_t lastLoginTime_;
110  
111      /**
112       * Guest device account to be removed.
113       */
114      bool guestToRemoved_;
115  
116      /**
117       * Device account information class default Constructor.
118       */
DeviceAccountInfo()119      DeviceAccountInfo()
120      {
121          id_ = DEVICE_ACCOUNT_ID_INVALID;
122          type_ = DeviceAccountType::DEVICE_ACCOUNT_TYPE_INVALID;
123          name_.clear();
124          iconPath_.clear();
125          state_ = DeviceAccountState::DEVICE_ACCOUNT_STATE_INVALID;
126          flags_ = 0;
127          creationTime_ = 0;
128          lastLoginTime_ = 0;
129          guestToRemoved_ = false;
130      }
131  
132      /**
133       * Device account information class Constructor.
134       */
DeviceAccountInfo(const std::int32_t accountId,const DeviceAccountType accountType,const std::string & accountName)135      DeviceAccountInfo(const std::int32_t accountId, const DeviceAccountType accountType, const std::string &accountName)
136          : id_(accountId), type_(accountType), name_(accountName)
137      {
138          iconPath_.clear();
139          state_ = DeviceAccountState::DEVICE_ACCOUNT_STATE_INVALID;
140          flags_ = 0;
141          creationTime_ = 0;
142          lastLoginTime_ = 0;
143          guestToRemoved_ = false;
144      }
145  
146      /**
147       * Device account information class Constructor.
148       */
DeviceAccountInfo(const std::int32_t accountId,const DeviceAccountType accountType,const std::string & accountName,const std::string & path)149      DeviceAccountInfo(const std::int32_t accountId, const DeviceAccountType accountType,
150          const std::string &accountName, const std::string &path)
151          : id_(accountId), type_(accountType), name_(accountName), iconPath_(path)
152      {
153          state_ = DeviceAccountState::DEVICE_ACCOUNT_STATE_INVALID;
154          flags_ = 0;
155          creationTime_ = 0;
156          lastLoginTime_ = 0;
157          guestToRemoved_ = false;
158      }
159  
160      /**
161       * Device account information class default Destructor.
162       */
~DeviceAccountInfo()163      ~DeviceAccountInfo() {}
164  
165      /**
166       * == redefinition
167       *
168       * @return whether id is equal
169       */
170      bool operator==(const DeviceAccountInfo &source)
171      {
172          return ((id_ == source.id_) && (id_ != DEVICE_ACCOUNT_ID_INVALID));
173      }
174  
175      /**
176       * write device account info to parcel
177       *
178       * @return 0 when succeeded, otherwise -1
179       */
WriteDataToParcel(MessageParcel & data)180      std::int32_t WriteDataToParcel(MessageParcel &data)
181      {
182          if (!data.WriteInt32(id_)) {
183              return ERROR_HAPPEN;
184          }
185  
186          if (!data.WriteInt32(static_cast<std::int32_t>(type_))) {
187              return ERROR_HAPPEN;
188          }
189  
190          if (!data.WriteString16(Str8ToStr16(name_))) {
191              return ERROR_HAPPEN;
192          }
193  
194          if (!data.WriteInt32(static_cast<std::int32_t>(state_))) {
195              return ERROR_HAPPEN;
196          }
197  
198          return ERROR_NONE;
199      }
200  
201      /**
202       * read device account info from parcel
203       *
204       */
ReadDataFromParcel(MessageParcel & data)205      void ReadDataFromParcel(MessageParcel &data)
206      {
207          id_ = data.ReadInt32();
208          type_ = static_cast<DeviceAccountType>(data.ReadInt32());
209          name_ = Str16ToStr8(data.ReadString16());
210          state_ = static_cast<DeviceAccountState>(data.ReadInt32());
211      }
212  };
213  } // namespace AccountSA
214  } // namespace OHOS
215  #endif // BASE_ACCOUNT_DEVICE_ACCOUNT_INFO_H
216