350 lines
13 KiB
Java
350 lines
13 KiB
Java
package com.ubt.jimu.transport.update;
|
||
|
||
import android.app.Activity;
|
||
import android.content.DialogInterface;
|
||
import android.text.TextUtils;
|
||
import android.util.Log;
|
||
import com.ubt.jimu.R;
|
||
import com.ubt.jimu.base.cache.Cache;
|
||
import com.ubt.jimu.base.db.DatabaseUtils;
|
||
import com.ubt.jimu.base.db.diy.DiyDBModel;
|
||
import com.ubt.jimu.base.entities.User;
|
||
import com.ubt.jimu.blockly.bean.BlocklyProject;
|
||
import com.ubt.jimu.blockly.bean.JimuMotion;
|
||
import com.ubt.jimu.blockly.bean.JimuSound;
|
||
import com.ubt.jimu.blockly.dao.BlocklyProjectDbHandler;
|
||
import com.ubt.jimu.blockly.dao.JimuMotionDbHandler;
|
||
import com.ubt.jimu.blockly.dao.JimuSoundDbHandler;
|
||
import com.ubt.jimu.diy.DiyRobotDbHandler;
|
||
import com.ubt.jimu.gen.BlocklyProjectDao;
|
||
import com.ubt.jimu.gen.DiyModelActionDao;
|
||
import com.ubt.jimu.gen.JimuMotionDao;
|
||
import com.ubt.jimu.gen.JimuSoundDao;
|
||
import com.ubt.jimu.gen.TransportFileDao;
|
||
import com.ubt.jimu.transport.dao.ConfigItemDbHandler;
|
||
import com.ubt.jimu.transport.dao.TransportFileDbHandler;
|
||
import com.ubt.jimu.transport.model.TransportFile;
|
||
import com.ubt.jimu.transport3.dao.DiyModelActionDBHandler;
|
||
import com.ubt.jimu.transport3.model.DiyModelAction;
|
||
import com.ubt.jimu.utils.LogUtils;
|
||
import com.ubtech.utils.FileHelper;
|
||
import com.ubtech.view.dialog.SimpleDialog;
|
||
import com.ubtrobot.log.ALog;
|
||
import io.reactivex.Observable;
|
||
import io.reactivex.Observer;
|
||
import io.reactivex.android.schedulers.AndroidSchedulers;
|
||
import io.reactivex.disposables.Disposable;
|
||
import io.reactivex.schedulers.Schedulers;
|
||
import java.io.File;
|
||
import java.util.Iterator;
|
||
import java.util.List;
|
||
import org.greenrobot.greendao.query.QueryBuilder;
|
||
import org.greenrobot.greendao.query.WhereCondition;
|
||
|
||
/* loaded from: classes2.dex */
|
||
public class MergeTool {
|
||
private static final String k = "MergeTool";
|
||
private DiyRobotDbHandler a = new DiyRobotDbHandler(DatabaseUtils.getDaoSession(true).d());
|
||
private BlocklyProjectDbHandler b;
|
||
private JimuSoundDbHandler c;
|
||
private JimuMotionDbHandler d;
|
||
private TransportFileDbHandler e;
|
||
private DiyModelActionDBHandler f;
|
||
private final String g;
|
||
private final String h;
|
||
private final String i;
|
||
private final String j;
|
||
|
||
public interface IMergeListener {
|
||
void a(boolean z);
|
||
|
||
void onFailed();
|
||
|
||
void onStart();
|
||
}
|
||
|
||
public MergeTool(String str, String str2) {
|
||
new ConfigItemDbHandler(DatabaseUtils.getDaoSession(true).b());
|
||
this.b = new BlocklyProjectDbHandler(DatabaseUtils.getDaoSession(true).a());
|
||
this.c = new JimuSoundDbHandler(DatabaseUtils.getDaoSession(true).p());
|
||
this.d = new JimuMotionDbHandler(DatabaseUtils.getDaoSession(true).o());
|
||
this.e = new TransportFileDbHandler(DatabaseUtils.getDaoSession(true).v());
|
||
this.f = DiyModelActionDBHandler.getInstance();
|
||
this.g = str;
|
||
this.i = str2;
|
||
this.h = DiyRobotDbHandler.getUserHome(str);
|
||
this.j = DiyRobotDbHandler.getUserHome(str2);
|
||
}
|
||
|
||
private boolean c() {
|
||
String str = this.h;
|
||
String str2 = this.j;
|
||
File file = new File(str);
|
||
File file2 = new File(str2);
|
||
if (!file.exists()) {
|
||
return false;
|
||
}
|
||
try {
|
||
FileHelper.a(file, file2);
|
||
return true;
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
return false;
|
||
}
|
||
}
|
||
|
||
private boolean d() {
|
||
QueryBuilder<DiyModelAction> queryBuilder = this.f.getQueryBuilder();
|
||
queryBuilder.a(DiyModelActionDao.Properties.UserId.a((Object) this.g), new WhereCondition[0]);
|
||
queryBuilder.a(1);
|
||
List<DiyModelAction> query = this.f.query(queryBuilder);
|
||
return query != null && query.size() > 0;
|
||
}
|
||
|
||
private boolean e() {
|
||
QueryBuilder<BlocklyProject> queryBuilder = this.b.getQueryBuilder();
|
||
queryBuilder.a(BlocklyProjectDao.Properties.UserId.a((Object) this.g), new WhereCondition[0]);
|
||
queryBuilder.a(1);
|
||
List<BlocklyProject> query = this.b.query(queryBuilder);
|
||
return query != null && query.size() > 0;
|
||
}
|
||
|
||
private boolean f() {
|
||
return this.a.hasDiyDbModel(this.g);
|
||
}
|
||
|
||
private boolean g() {
|
||
QueryBuilder<JimuMotion> queryBuilder = this.d.getQueryBuilder();
|
||
queryBuilder.a(JimuMotionDao.Properties.UserId.a((Object) this.g), new WhereCondition[0]);
|
||
queryBuilder.a(1);
|
||
List<JimuMotion> query = this.d.query(queryBuilder);
|
||
return query != null && query.size() > 0;
|
||
}
|
||
|
||
private boolean h() {
|
||
QueryBuilder<TransportFile> queryBuilder = this.e.getQueryBuilder();
|
||
queryBuilder.a(TransportFileDao.Properties.UserId.a((Object) this.g), new WhereCondition[0]);
|
||
queryBuilder.a(1);
|
||
List<TransportFile> query = this.e.query(queryBuilder);
|
||
return query != null && query.size() > 0;
|
||
}
|
||
|
||
private boolean i() {
|
||
QueryBuilder<JimuSound> queryBuilder = this.c.getQueryBuilder();
|
||
queryBuilder.a(JimuSoundDao.Properties.UserId.a((Object) this.g), new WhereCondition[0]);
|
||
queryBuilder.a(1);
|
||
List<JimuSound> query = this.c.query(queryBuilder);
|
||
return query != null && query.size() > 0;
|
||
}
|
||
|
||
private void j() {
|
||
boolean m = m();
|
||
boolean l = l();
|
||
boolean o = o();
|
||
boolean n = n();
|
||
boolean p = p();
|
||
k();
|
||
if (m && l && o && n && p) {
|
||
ALog.a(k).d("merge data success");
|
||
} else {
|
||
ALog.a(k).d("merge data failed");
|
||
}
|
||
}
|
||
|
||
private void k() {
|
||
List<DiyModelAction> userActions = this.f.getUserActions(this.g);
|
||
if (userActions == null || userActions.size() == 0) {
|
||
return;
|
||
}
|
||
Iterator<DiyModelAction> it = userActions.iterator();
|
||
while (it.hasNext()) {
|
||
it.next().setUserId(this.i);
|
||
}
|
||
this.f.updateInTx(userActions);
|
||
}
|
||
|
||
private boolean l() {
|
||
List<BlocklyProject> blocklyProject = this.b.getBlocklyProject(this.g);
|
||
if (blocklyProject != null && blocklyProject.size() > 0) {
|
||
Iterator<BlocklyProject> it = blocklyProject.iterator();
|
||
while (it.hasNext()) {
|
||
it.next().setUserId(this.i);
|
||
}
|
||
try {
|
||
this.b.updateInTx(blocklyProject);
|
||
} catch (Exception e) {
|
||
e.getMessage();
|
||
Log.e(k, "合并BlocklyPro到目标用户失败" + e.getMessage());
|
||
return false;
|
||
}
|
||
}
|
||
return true;
|
||
}
|
||
|
||
private boolean m() {
|
||
List<DiyDBModel> userModels = this.a.getUserModels(this.g);
|
||
if (userModels != null && userModels.size() > 0) {
|
||
for (DiyDBModel diyDBModel : userModels) {
|
||
diyDBModel.setModelCreatedId(this.i);
|
||
if (!TextUtils.isEmpty(diyDBModel.getFilePath())) {
|
||
diyDBModel.setFilePath(diyDBModel.getFilePath().replace(this.g, this.i));
|
||
}
|
||
}
|
||
try {
|
||
this.a.updateInTx(userModels);
|
||
} catch (Exception e) {
|
||
e.getMessage();
|
||
Log.e(k, "合并DiyDBModel到目标用户失败:" + e.getMessage());
|
||
return false;
|
||
}
|
||
}
|
||
return true;
|
||
}
|
||
|
||
private boolean n() {
|
||
List<JimuMotion> userModelMotions = this.d.getUserModelMotions(this.g);
|
||
if (userModelMotions != null && userModelMotions.size() > 0) {
|
||
Iterator<JimuMotion> it = userModelMotions.iterator();
|
||
while (it.hasNext()) {
|
||
it.next().setUserId(this.i);
|
||
}
|
||
try {
|
||
this.d.updateInTx(userModelMotions);
|
||
} catch (Exception e) {
|
||
e.getMessage();
|
||
Log.e(k, "合并JimuMotion到目标用户失败:" + e.getMessage());
|
||
return false;
|
||
}
|
||
}
|
||
return true;
|
||
}
|
||
|
||
private boolean o() {
|
||
List<JimuSound> jimuSounds = this.c.getJimuSounds(this.g);
|
||
if (jimuSounds != null && jimuSounds.size() > 0) {
|
||
Iterator<JimuSound> it = jimuSounds.iterator();
|
||
while (it.hasNext()) {
|
||
it.next().setUserId(this.i);
|
||
}
|
||
try {
|
||
this.c.updateInTx(jimuSounds);
|
||
} catch (Exception e) {
|
||
e.getMessage();
|
||
Log.e(k, "合并JimuSound到目标用户失败:" + e.getMessage());
|
||
return false;
|
||
}
|
||
}
|
||
return true;
|
||
}
|
||
|
||
private boolean p() {
|
||
List<TransportFile> files = this.e.getFiles(this.g);
|
||
if (files != null && files.size() > 0) {
|
||
for (TransportFile transportFile : files) {
|
||
transportFile.setUserId(this.i);
|
||
if ("1".equals(transportFile.getModelType()) && !"local".equals(Cache.getInstance().getUserId())) {
|
||
this.e.deleteTransportFile(transportFile);
|
||
}
|
||
}
|
||
try {
|
||
this.e.updateInTx(files);
|
||
} catch (Exception e) {
|
||
e.getMessage();
|
||
Log.e(k, "合并TransportFile到目标用户失败:" + e.getMessage());
|
||
return false;
|
||
}
|
||
}
|
||
return true;
|
||
}
|
||
|
||
public void a(IMergeListener iMergeListener) {
|
||
if (iMergeListener != null) {
|
||
iMergeListener.onStart();
|
||
}
|
||
if (!c()) {
|
||
if (iMergeListener != null) {
|
||
iMergeListener.onFailed();
|
||
return;
|
||
}
|
||
return;
|
||
}
|
||
j();
|
||
File file = new File(this.h);
|
||
if (file.exists()) {
|
||
FileHelper.a(file);
|
||
}
|
||
if (iMergeListener != null) {
|
||
iMergeListener.a(true);
|
||
}
|
||
}
|
||
|
||
public boolean a() {
|
||
return f() || d() || e() || g() || i() || h();
|
||
}
|
||
|
||
public static void a(Activity activity, final IMergeListener iMergeListener) {
|
||
User user = Cache.getInstance().getUser();
|
||
if (user == null) {
|
||
if (iMergeListener != null) {
|
||
iMergeListener.a(false);
|
||
return;
|
||
}
|
||
return;
|
||
}
|
||
MergeTool mergeTool = new MergeTool("local", String.valueOf(user.getUserId()));
|
||
if (!mergeTool.a()) {
|
||
if (iMergeListener != null) {
|
||
iMergeListener.a(true);
|
||
}
|
||
} else {
|
||
SimpleDialog a = new SimpleDialog.Builder(activity).a(true).a(activity.getString(R.string.ok)).b(activity.getString(R.string.cancel)).a((CharSequence) activity.getString(R.string.tips_merge_data)).c(R.mipmap.course_miss_parts).a(new DialogInterface.OnClickListener() { // from class: com.ubt.jimu.transport.update.MergeTool.2
|
||
@Override // android.content.DialogInterface.OnClickListener
|
||
public void onClick(final DialogInterface dialogInterface, int i) {
|
||
Observable.just("merge").observeOn(AndroidSchedulers.a()).subscribeOn(Schedulers.b()).subscribe(new Observer<String>() { // from class: com.ubt.jimu.transport.update.MergeTool.2.1
|
||
@Override // io.reactivex.Observer
|
||
/* renamed from: a, reason: merged with bridge method [inline-methods] */
|
||
public void onNext(String str) {
|
||
ALog.a(MergeTool.k).d(str);
|
||
LogUtils.c("数据合并:onNext");
|
||
dialogInterface.dismiss();
|
||
}
|
||
|
||
@Override // io.reactivex.Observer
|
||
public void onComplete() {
|
||
LogUtils.c("数据合并:onComplete");
|
||
}
|
||
|
||
@Override // io.reactivex.Observer
|
||
public void onError(Throwable th) {
|
||
th.printStackTrace();
|
||
Log.e(MergeTool.k, th.getMessage());
|
||
LogUtils.c("数据合并:onError");
|
||
dialogInterface.dismiss();
|
||
}
|
||
|
||
@Override // io.reactivex.Observer
|
||
public void onSubscribe(Disposable disposable) {
|
||
AnonymousClass2 anonymousClass2 = AnonymousClass2.this;
|
||
MergeTool.this.a(iMergeListener);
|
||
}
|
||
});
|
||
}
|
||
}).b(new DialogInterface.OnClickListener() { // from class: com.ubt.jimu.transport.update.MergeTool.1
|
||
@Override // android.content.DialogInterface.OnClickListener
|
||
public void onClick(DialogInterface dialogInterface, int i) {
|
||
ALog.a(MergeTool.k).d("用户取消合并数据,将不会删除本地数据");
|
||
dialogInterface.dismiss();
|
||
IMergeListener iMergeListener2 = IMergeListener.this;
|
||
if (iMergeListener2 != null) {
|
||
iMergeListener2.a(false);
|
||
}
|
||
}
|
||
}).a();
|
||
if (activity.isFinishing() || activity.isDestroyed()) {
|
||
return;
|
||
}
|
||
a.show();
|
||
}
|
||
}
|
||
}
|