package com.ubt.jimu.blockly.dao; import android.text.TextUtils; import android.util.Log; import com.google.gson.reflect.TypeToken; import com.ubt.jimu.JimuApplication; import com.ubt.jimu.base.cache.Cache; import com.ubt.jimu.base.cache.SharePreferenceHelper; import com.ubt.jimu.base.db.AbstractDaoHandler; import com.ubt.jimu.base.db.DatabaseUtils; import com.ubt.jimu.blockly.JimuRobot; import com.ubt.jimu.blockly.Utils; import com.ubt.jimu.blockly.bean.JimuSound; import com.ubt.jimu.blockly.feature.audio.AudioParams; import com.ubt.jimu.gen.JimuSoundDao; import com.ubt.jimu.transport.Transporter; import com.ubt.jimu.transport3.dao.TransportFileDbHandler2; import com.ubt.jimu.unity.ModelType; import com.ubt.jimu.utils.ExternalOverFroyoUtils; import com.ubt.jimu.utils.JsonHelper; import com.ubt.jimu.utils.LogUtils; import com.ubtech.utils.FileHelper; import java.io.File; import java.util.ArrayList; import java.util.List; import org.greenrobot.greendao.query.QueryBuilder; import org.greenrobot.greendao.query.WhereCondition; /* loaded from: classes.dex */ public class JimuSoundDbHandler extends AbstractDaoHandler { private static final String TAG = "JimuSoundDbHandler"; private static JimuSoundDbHandler instance; public JimuSoundDbHandler(JimuSoundDao jimuSoundDao) { super(jimuSoundDao); } public static synchronized JimuSoundDbHandler getInstance() { JimuSoundDbHandler jimuSoundDbHandler; synchronized (JimuSoundDbHandler.class) { if (instance == null) { instance = new JimuSoundDbHandler(DatabaseUtils.getDaoSession(true).p()); } jimuSoundDbHandler = instance; } return jimuSoundDbHandler; } public static void update2Version3() { String[] list; SharePreferenceHelper sharePreferenceHelper = new SharePreferenceHelper(); if (sharePreferenceHelper.getBoolean("jimu_sound_update", true).booleanValue()) { String str = ExternalOverFroyoUtils.a(JimuApplication.l(), (ModelType) null) + "users" + File.separator; File file = new File(str); if (!file.exists() || (list = file.list()) == null || list.length == 0) { return; } JimuSoundDbHandler jimuSoundDbHandler = new JimuSoundDbHandler(DatabaseUtils.getDaoSession(true).p()); for (String str2 : list) { String str3 = str + str2 + File.separator + TransportFileDbHandler2.DIR_SOUNDS + File.separator + JimuRobot.SOUNDS; List list2 = (List) JsonHelper.a(FileHelper.b(str3, ""), new TypeToken>() { // from class: com.ubt.jimu.blockly.dao.JimuSoundDbHandler.2 }.getType()); if (list2 != null && list2.size() != 0) { for (JimuSound jimuSound : list2) { jimuSound.setUserId(str2); jimuSound.setIsDeleted("0"); } try { jimuSoundDbHandler.insertOrUpdateInTx(list2); new File(str3).delete(); } catch (Exception e) { e.printStackTrace(); Log.e(TAG, "迁移用户:" + str2 + " 的录音数据到数据表失败:" + e.getMessage()); } } } sharePreferenceHelper.put("jimu_sound_update", false); } } public boolean deleteJimuSound(JimuSound jimuSound) { JimuSound jimuSound2; if (jimuSound == null || (jimuSound2 = getJimuSound(jimuSound)) == null) { return false; } delete(jimuSound2); File file = new File(jimuSound.getLocalFilePath()); if (file.exists()) { file.delete(); } String parent = file.getParent(); LogUtils.c("path:" + parent); File file2 = new File(parent); if (!file2.exists() || !file2.isDirectory()) { return true; } file2.delete(); return true; } public String getDefaultSoundName(String str, String str2) { List jimuSounds = getJimuSounds(str); if (jimuSounds == null || jimuSounds.size() == 0 || TextUtils.isEmpty(str2)) { return str2 + "1"; } ArrayList arrayList = new ArrayList(); for (JimuSound jimuSound : jimuSounds) { if ("1".equals(jimuSound.getIsDeleted())) { arrayList.add(jimuSound); } } if (arrayList.size() > 0) { jimuSounds.removeAll(arrayList); } int[] iArr = new int[jimuSounds.size()]; int i = 0; for (JimuSound jimuSound2 : jimuSounds) { if (jimuSound2.getDescription().startsWith(str2)) { String substring = jimuSound2.getDescription().substring(str2.length(), jimuSound2.getDescription().length()); if (TextUtils.isDigitsOnly(substring) && !TextUtils.isEmpty(substring)) { iArr[i] = Integer.parseInt(substring); i++; } } } return Utils.getDefaultName(iArr, i, str2); } public JimuSound getJimuSound(JimuSound jimuSound) { String userId = Cache.getInstance().getUserId(); if (TextUtils.isEmpty(userId)) { userId = "local"; } try { QueryBuilder k = this.dao.k(); if (!TextUtils.isEmpty(jimuSound.getAudioId())) { k.a(JimuSoundDao.Properties.UserId.a((Object) userId), JimuSoundDao.Properties.AudioId.a((Object) jimuSound.getAudioId()), JimuSoundDao.Properties.IsDeleted.a((Object) 0)); } else { if (TextUtils.isEmpty(jimuSound.getKey())) { return null; } k.a(JimuSoundDao.Properties.UserId.a((Object) userId), JimuSoundDao.Properties.IsDeleted.a((Object) 0), JimuSoundDao.Properties.Key.a((Object) jimuSound.getKey())); } return (JimuSound) k.c(); } catch (Exception e) { e.printStackTrace(); return null; } } public List getJimuSounds(String str) { List list; try { QueryBuilder k = this.dao.k(); k.a(JimuSoundDao.Properties.UserId.a((Object) str), new WhereCondition[0]); list = k.b(); } catch (Exception e) { e.printStackTrace(); list = null; } return list == null ? new ArrayList() : list; } public List getOfflineQiNiuFile() { String userId = Cache.getInstance().getUserId(); if (TextUtils.isEmpty(userId)) { userId = "local"; } List list = null; try { QueryBuilder k = this.dao.k(); k.a(JimuSoundDao.Properties.UserId.a((Object) userId), JimuSoundDao.Properties.IsUploadQiNiu.a((Object) 0), JimuSoundDao.Properties.IsSyncUbtService.a((Object) 0)); list = k.b(); } catch (Exception e) { e.printStackTrace(); } return list == null ? new ArrayList() : list; } public List getOfflineServiceFile() { String userId = Cache.getInstance().getUserId(); if (TextUtils.isEmpty(userId)) { userId = "local"; } List list = null; try { QueryBuilder k = this.dao.k(); k.a(JimuSoundDao.Properties.UserId.a((Object) userId), JimuSoundDao.Properties.IsUploadQiNiu.a((Object) 1), JimuSoundDao.Properties.IsSyncUbtService.a((Object) 0)); list = k.b(); } catch (Exception e) { e.printStackTrace(); } return list == null ? new ArrayList() : list; } public boolean insertOrUpdateFromService(JimuSound jimuSound) { if (jimuSound == null) { return false; } if (TextUtils.isEmpty(jimuSound.getAudioId()) && TextUtils.isEmpty(jimuSound.getKey())) { return false; } JimuSound jimuSound2 = getJimuSound(jimuSound); if (jimuSound2 != null) { jimuSound2.setLastUploadTime(jimuSound.getLastUploadTime()); jimuSound2.setAudioId(jimuSound.getAudioId()); jimuSound2.setDescription(jimuSound.getDescription()); jimuSound2.setDuration(jimuSound.getDuration()); jimuSound2.setIsDeleted("0"); jimuSound2.setIsSyncUbtService(true); jimuSound2.setIsUploadQiNiu(true); insertOrUpdate(jimuSound2); } else { JimuSound jimuSound3 = new JimuSound(); jimuSound3.setUserId(jimuSound.getUserId()); jimuSound3.setIsDeleted("0"); jimuSound3.setKey(jimuSound.getAudioId()); jimuSound3.setIsSyncUbtService(true); jimuSound3.setIsUploadQiNiu(true); jimuSound3.setDescription(jimuSound.getDescription()); jimuSound3.setDuration(jimuSound.getDuration()); jimuSound3.setIsSyncUbtService(true); jimuSound3.setIsUploadQiNiu(true); jimuSound3.setType(jimuSound.getType()); jimuSound3.setLastUploadTime(jimuSound.getLastUploadTime()); jimuSound3.setAudioId(jimuSound.getAudioId()); insertOrUpdate(jimuSound3); } return true; } public boolean nameExist(String str, String str2) { List jimuSounds = getJimuSounds(str); if (jimuSounds != null && jimuSounds.size() != 0) { for (JimuSound jimuSound : jimuSounds) { if (jimuSound.getDescription().equals(str2) && !"1".equals(jimuSound.getIsDeleted())) { return true; } } } return false; } public boolean updateJimuSoundUbtServiceState(JimuSound jimuSound, boolean z) { JimuSound selectUnique; if (jimuSound == null || (selectUnique = selectUnique(jimuSound)) == null) { return false; } selectUnique.setSyncUbtService(z); insertOrUpdate(selectUnique); return true; } public boolean updateQiNiuSyncState(JimuSound jimuSound, boolean z) { JimuSound selectUnique; if (jimuSound == null || (selectUnique = selectUnique(jimuSound)) == null) { return false; } selectUnique.setUploadQiNiu(z); insertOrUpdate(selectUnique); return true; } public void updateToVersion3(String str, String str2) { String str3 = str + JimuRobot.SOUNDS; File file = new File(str3); if (file.exists()) { List list = (List) JsonHelper.a(FileHelper.b(str3, ""), new TypeToken>() { // from class: com.ubt.jimu.blockly.dao.JimuSoundDbHandler.1 }.getType()); if (list == null || list.size() == 0) { Transporter.b().a(file); file.delete(); return; } for (JimuSound jimuSound : list) { jimuSound.setUserId(str2); if ("1".equals(jimuSound.getIsDeleted())) { jimuSound.setIsDeleted(jimuSound.getIsDeleted()); } } try { insertOrUpdateInTx(list); Transporter.b().a(file); file.delete(); } catch (Exception e) { e.getMessage(); Log.e(TAG, "updateToVersion3 on error: " + e.getMessage()); } } } public boolean uploadDeleteState(JimuSound jimuSound, boolean z) { JimuSound selectUnique; if (jimuSound == null || (selectUnique = selectUnique(jimuSound)) == null) { return false; } selectUnique.setIsDeleted(z ? "1" : "0"); update(jimuSound); return true; } @Override // com.ubt.jimu.base.db.AbstractDaoHandler, com.ubt.jimu.base.db.IDaoHandler public JimuSound selectUnique(JimuSound jimuSound) { return getJimuSound(jimuSound.getUserId(), jimuSound.getKey()); } public List getJimuSounds(String str, String str2) { updateToVersion3(str, str2); List jimuSounds = getJimuSounds(str2); ArrayList arrayList = new ArrayList(); if (jimuSounds != null && jimuSounds.size() != 0) { for (JimuSound jimuSound : jimuSounds) { if (!"1".equals(jimuSound.getIsDeleted())) { String key = jimuSound.getKey(); if (TextUtils.isEmpty(key)) { key = jimuSound.getAudioId(); } File file = new File(str + key + File.separator + key + AudioParams.AAC); if (file.exists() && file.isFile()) { arrayList.add(jimuSound); } } } } return arrayList; } public JimuSound getJimuSound(String str, String str2) { try { QueryBuilder k = this.dao.k(); k.a(JimuSoundDao.Properties.UserId.a((Object) str), JimuSoundDao.Properties.Key.a((Object) str2)); return (JimuSound) k.c(); } catch (Exception e) { e.printStackTrace(); return null; } } }