AccountManager.java
6.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package com.sw.laryngoscope.manager;
import static com.sw.laryngoscope.common.InitParam.USER_TYPE_ADMIN;
import static com.sw.laryngoscope.common.InitParam.USER_TYPE_GUEST;
import static com.sw.laryngoscope.common.InitParam.USER_TYPE_USER;
import static com.sw.laryngoscope.common.InitParam.name_guest;
import com.sw.laryngoscope.db.AccountInfoDB;
import com.sw.laryngoscope.utils.Logger;
import org.litepal.LitePal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* @Description 用来保存用户在各个模式运动前设置的信息
* @Author
* @Time 2019/09/09
*/
public class AccountManager {
public List<AccountInfoDB> accountInfolist = new ArrayList<>();
private static AccountManager instance;
public static String admin_name = "Administrator";
public static int admin_name_id = 1;
public static AccountManager getInstance() {
if (instance == null) {
synchronized (AccountManager.class) {
if (instance == null) {
instance = new AccountManager();
}
}
}
return instance;
}
public void initList() {
accountInfolist = LitePal.findAll(AccountInfoDB.class);
if ( accountInfolist.size() == 0 ) {
new AccountInfoDB(admin_name, "00000000", USER_TYPE_ADMIN).save();
//new AccountInfoDB("sdsd", "00000000", USER_TYPE_USER).save();
}
clearAllLogin();
clearAllSel();
}
public String findUserPsd( String name ) {
List<AccountInfoDB> list = LitePal.where("name = ?", name).find( AccountInfoDB.class);
return list.get(0).getPassword();
}
public AccountInfoDB findUserData( String name ) {
AccountInfoDB mDb = null;
List<AccountInfoDB> list = LitePal.where("name = ?", name).find( AccountInfoDB.class);
if ( list.size() > 0 ) {
mDb = list.get(0);
}
return mDb;
}
public List<AccountInfoDB> findAll() {
accountInfolist = LitePal.findAll(AccountInfoDB.class);
return accountInfolist;
}
public void deleteUser( String user ) {
Iterator<AccountInfoDB> iterator = accountInfolist.iterator();
while ( iterator.hasNext() ) {
AccountInfoDB deleteInfo = iterator.next();
if ( deleteInfo.getName().equals(user) ) {
iterator.remove();//使用迭代器的删除方法删除
deleteInfo.delete();
Logger.d("===deleteFile==1=");
break;
}
}
}
public void clearAllLogin() {
Iterator<AccountInfoDB> iterator = accountInfolist.iterator();
while ( iterator.hasNext() ) {
AccountInfoDB deleteInfo = iterator.next();
if ( deleteInfo.isLogin() ) {
deleteInfo.setLogin(false);
deleteInfo.saveOrUpdate("name = ?", deleteInfo.getName());
Logger.d("===clearAllLogin==1=");
}
}
}
public void clearAllSel() {
Iterator<AccountInfoDB> iterator = accountInfolist.iterator();
while ( iterator.hasNext() ) {
AccountInfoDB deleteInfo = iterator.next();
if ( deleteInfo.getIsSelect() == 1 ) {
deleteInfo.setIsSelect(0);
deleteInfo.saveOrUpdate("name = ?", deleteInfo.getName());
Logger.d("===clearAllSel==1=");
}
}
/*AccountInfoDB tmp = accountInfolist.get(accountInfolist.size() - 1);
tmp.setIsSelect(1);
tmp.saveOrUpdate("name = ?", tmp.getName());*/
}
public boolean isUserDbOrNoLogin() {
accountInfolist = LitePal.findAll(AccountInfoDB.class);
boolean isLogin = false;
Iterator<AccountInfoDB> iterator = accountInfolist.iterator();
while ( iterator.hasNext() ) {
AccountInfoDB info = iterator.next();
isLogin = info.isLogin() || isLogin;
if ( info.isLogin() && info.getUserType() == USER_TYPE_USER ) {
return true;
}
}
if ( isLogin ) {
return false;
} else {
return true;
}
}
public AccountInfoDB getLoginAcc() {
accountInfolist = LitePal.findAll(AccountInfoDB.class);
AccountInfoDB ret = new AccountInfoDB(-1000, name_guest, "", USER_TYPE_GUEST, 0, true);
Iterator<AccountInfoDB> iterator = accountInfolist.iterator();
while ( iterator.hasNext() ) {
AccountInfoDB info = iterator.next();
if ( info.isLogin() ) {
return info;
}
}
return ret;
}
public boolean isAdmin( AccountInfoDB tmp ) {
if ( tmp.getUserType() == USER_TYPE_ADMIN ) {
return true;
}
return false;
}
public boolean isAdmin() {
accountInfolist = LitePal.findAll(AccountInfoDB.class);
Iterator<AccountInfoDB> iterator = accountInfolist.iterator();
while ( iterator.hasNext() ) {
AccountInfoDB info = iterator.next();
Logger.d("++++++++++++isAdmin+++++++" + info.toString());
if ( info.isLogin() && info.getUserType() == USER_TYPE_ADMIN ) {
return true;
}
}
return false;
}
public boolean isAdvance( AccountInfoDB tmp ) {
if ( tmp.getUserType() == USER_TYPE_USER ) {
return true;
}
return false;
}
public boolean isUser( AccountInfoDB tmp ) {
if ( tmp.getUserType() == USER_TYPE_USER ) {
return true;
}
return false;
}
public boolean isLogin() {
accountInfolist = LitePal.findAll(AccountInfoDB.class);
Iterator<AccountInfoDB> iterator = accountInfolist.iterator();
while ( iterator.hasNext() ) {
AccountInfoDB info = iterator.next();
if ( info.isLogin() ) {
return true;
}
}
return false;
}
public int getListLen() {
accountInfolist = LitePal.findAll(AccountInfoDB.class);
if ( accountInfolist != null ) {
return accountInfolist.size();
}
return 0;
}
}