package com.basic.security.manager.impl.cblite;
|
|
import android.graphics.Bitmap;
|
import android.graphics.BitmapFactory;
|
import android.util.Log;
|
|
import com.basic.security.R;
|
import com.basic.security.activity.MainActivity;
|
import com.basic.security.base.BaseApplication;
|
import com.basic.security.fragment.ToolbarFragment;
|
import com.basic.security.manager.impl.sqlite.SlAccountManager;
|
import com.basic.security.utils.Constants;
|
import com.couchbase.lite.Blob;
|
import com.couchbase.lite.Document;
|
import com.couchbase.lite.Expression;
|
import com.basic.security.model.ModelAdapter;
|
|
import java.util.List;
|
|
|
public class AccountManager extends BaseManager {
|
|
private static ModelAdapter adminAccount;
|
|
public static ModelAdapter getAdminAccount() {
|
if (Constants.useCouchbase) {
|
return adminAccount;
|
} else {
|
return SlAccountManager.getAdminAccount();
|
}
|
}
|
|
|
public static void setAdminAccount(ModelAdapter admin) {
|
if (Constants.useCouchbase) {
|
if (admin != null) {
|
try {
|
((MainActivity) (BaseApplication.getApplication().activity)).lastAdminClickTime = System.currentTimeMillis();
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
AccountManager.adminAccount = admin;
|
|
if (adminAccount != null) {
|
setAdminPhoto();
|
}
|
} else {
|
SlAccountManager.setAdminAccount(admin);
|
}
|
}
|
|
private static void setAdminPhoto() {
|
try {
|
MainActivity activity = (MainActivity) BaseApplication.getApplication().activity;
|
if (activity == null) {
|
return;
|
}
|
String a = adminAccount.getString("person_id");
|
ModelAdapter admin_person = PersonManager.findPersonById(a);
|
if (admin_person == null) {
|
return;
|
}
|
if (!"person".equals(admin_person.getString("table"))) {
|
return;
|
}
|
String name = admin_person.getString("name");
|
Log.e("name", name);
|
Bitmap b = null;
|
|
byte[] blob = admin_person.getBlob("camera_image_path");
|
byte[] data = null;
|
if (blob != null) {
|
data = blob;
|
if (data != null) {
|
b = BitmapFactory.decodeByteArray(data, 0, data.length);
|
}
|
}
|
if (b == null) {
|
b = BitmapFactory.decodeResource(activity.getResources(), R.drawable.u1280);
|
}
|
|
if (b != null) {
|
((ToolbarFragment) activity.fragment_toolbar).admin_photo.setImageBitmap(b);
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
/*保存账号*/
|
public static ModelAdapter getAccount(String account, String password) {
|
if (Constants.useCouchbase) {
|
List<ModelAdapter> list = findList(Expression.property("table").equalTo(Expression.string("user")).and(Expression.property("username").equalTo(Expression.string(account))));
|
|
for (ModelAdapter d : list) {
|
if (password.equals(d.getString("password"))) {
|
return d;
|
}
|
}
|
return null;
|
} else {
|
return SlAccountManager.getAccount(account, password);
|
}
|
|
}
|
|
// 管理员是否已经登录
|
public static boolean adminLoggedIn() {
|
if (Constants.useCouchbase) {
|
return adminAccount != null;
|
} else {
|
return SlAccountManager.adminLoggedIn();
|
}
|
}
|
|
/**
|
* 根据管理员获取账号密码
|
*/
|
|
public static String getAdminPassword(String admin) {
|
if (Constants.useCouchbase) {
|
Expression expression = Expression.property("table").equalTo(Expression.string("user"));
|
expression = expression.and(Expression.property("username").equalTo(Expression.string(admin)));
|
return adminAccount.getString("password");
|
} else {
|
return SlAccountManager.getAdminPassword(admin);
|
}
|
}
|
|
|
public static String getCurrentAdminPassword() {
|
if (Constants.useCouchbase) {
|
return adminAccount.getString("password");
|
} else {
|
return SlAccountManager.getCurrentAdminPassword();
|
}
|
}
|
|
public static void setAdminId(String person_id) {
|
if (Constants.useCouchbase) {
|
Expression e = Expression.property("table").equalTo(Expression.string("user")).and(Expression.property("person_id").equalTo(Expression.string(person_id)));
|
ModelAdapter d = findOne(e);
|
if ("user".equals(d.getString("table")) && person_id.equals(d.getString("person_id"))) {
|
setAdminAccount(d);
|
}
|
} else {
|
SlAccountManager.setAdminId(person_id);
|
}
|
}
|
|
public static void saveAccount(ModelAdapter modelAdapter) {
|
if (Constants.useCouchbase) {
|
BaseManager.save(modelAdapter);
|
} else {
|
SlAccountManager.save(modelAdapter);
|
}
|
}
|
}
|