diff --git a/app/build.gradle b/app/build.gradle index 70b92c3..4672e37 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -88,7 +88,7 @@ dependencies { compile 'net.jpountz.lz4:lz4:1.3.0' - compile 'com.google.android.gms:play-services-analytics:11.0.2' + compile 'com.google.android.gms:play-services-analytics:11.0.4' compile 'com.h6ah4i.android.widget.verticalseekbar:verticalseekbar:0.7.2' } apply plugin: 'com.google.gms.google-services' \ No newline at end of file diff --git a/app/src/main/java/com/lazyeraser/imas/cgss/utils/DBHelper.java b/app/src/main/java/com/lazyeraser/imas/cgss/utils/DBHelper.java index 5048f68..e671ed9 100644 --- a/app/src/main/java/com/lazyeraser/imas/cgss/utils/DBHelper.java +++ b/app/src/main/java/com/lazyeraser/imas/cgss/utils/DBHelper.java @@ -88,6 +88,16 @@ public static DBHelper with(BaseActivity context, String dbName){ return instances.get(dbName); } + public static void refresh(BaseActivity context, String dbName){ + if (instances == null) { + synchronized (DBHelper.class) { + if (instances == null) + instances = new HashMap<>(); + } + } + instances.put(dbName, new DBHelper(context, context.getFilesDir().getAbsolutePath() + "/" + dbName)); + } + public static DBHelper getInstance(){ if (instances == null || instances.get(DB_NAME) == null) { throw new NullPointerException("No instance of DBHelper"); @@ -104,6 +114,7 @@ public static DBHelper getInstance(String dbName){ /*====================================*/ + private DBHelper(Context context, String dbName) { super(context, dbName, null, DB_VERSION); onCreate(getReadableDatabase()); @@ -112,15 +123,16 @@ private DBHelper(Context context, String dbName) { @Override public void onCreate(SQLiteDatabase db) { - List SQLs = new ArrayList<>(); - SQLs.add("create table if not exists " + TABLE_NAME_Card + "(id integer primary key, json VARCHAR)"); - SQLs.add("create table if not exists " + TABLE_NAME_Chara_Index + "(id integer primary key, json VARCHAR)"); - SQLs.add("create table if not exists " + TABLE_NAME_Chara_Detail + "(id integer primary key, json VARCHAR)"); - SQLs.add("create table if not exists " + TABLE_NAME_Translation + "(origin VARCHAR primary key, translate VARCHAR)"); - for (String sql : SQLs) { - db.execSQL(sql); + if (getDatabaseName().equals(DB_NAME)){ + List SQLs = new ArrayList<>(); + SQLs.add("create table if not exists " + TABLE_NAME_Card + "(id integer primary key, json VARCHAR)"); + SQLs.add("create table if not exists " + TABLE_NAME_Chara_Index + "(id integer primary key, json VARCHAR)"); + SQLs.add("create table if not exists " + TABLE_NAME_Chara_Detail + "(id integer primary key, json VARCHAR)"); + SQLs.add("create table if not exists " + TABLE_NAME_Translation + "(origin VARCHAR primary key, translate VARCHAR)"); + for (String sql : SQLs) { + db.execSQL(sql); + } } - } public void beginTran(){ diff --git a/app/src/main/java/com/lazyeraser/imas/cgss/utils/FileHelper.java b/app/src/main/java/com/lazyeraser/imas/cgss/utils/FileHelper.java index 1efc831..7ef0134 100644 --- a/app/src/main/java/com/lazyeraser/imas/cgss/utils/FileHelper.java +++ b/app/src/main/java/com/lazyeraser/imas/cgss/utils/FileHelper.java @@ -29,7 +29,7 @@ public static boolean writeFile(byte[] file, String filePath, String fileName) { File destDir = new File(filePath); if (!destDir.exists()) { if (!destDir.mkdir()){ - throw new Exception("目录创建失败,请检查文件夹权限等问题"); + throw new Exception("mkdir fail"); } } File destFile = new File(destDir, fileName); @@ -52,7 +52,6 @@ public static boolean writeFile(byte[] file, String filePath, String fileName) { e.printStackTrace(); } } - Utils.mPrint("写入文件成功" + filePath + "/" + fileName); return true; } diff --git a/app/src/main/java/com/lazyeraser/imas/cgss/view/MainActivity.java b/app/src/main/java/com/lazyeraser/imas/cgss/view/MainActivity.java index 3e879e7..c7ea832 100644 --- a/app/src/main/java/com/lazyeraser/imas/cgss/view/MainActivity.java +++ b/app/src/main/java/com/lazyeraser/imas/cgss/view/MainActivity.java @@ -129,8 +129,10 @@ protected void onCreate(Bundle savedInstanceState) { switchFrag(cardListFrag); if (umi.getSP(SharedHelper.KEY_AUTO_APP)){ + umi.makeToast(R.string.settings_auto_app); checkUpdate(false); }else if (umi.getSP(SharedHelper.KEY_AUTO_DATA)){ + umi.makeToast(R.string.settings_auto_data); mainViewModel.checkDataUpdate(); } // 监听语言设置改变 @@ -172,16 +174,15 @@ private void askRestart(){ private void checkUpdate(boolean hint){ needUpdateHint = hint; - if (hint) { - umi.showLoading(); - } + umi.showLoading(); if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions((Activity) mContext, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1); }else { updateManager.checkUpdate(hint, b -> { umi.dismissLoading(); if(!b){ - if (umi.getSP(SharedHelper.KEY_AUTO_DATA)){ + if (umi.getSP(SharedHelper.KEY_AUTO_DATA) && !needUpdateHint){ + umi.makeToast(R.string.settings_auto_data); mainViewModel.checkDataUpdate(); } } @@ -197,7 +198,8 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis updateManager.checkUpdate(needUpdateHint, b -> { umi.dismissLoading(); if(!b){ - if (umi.getSP(SharedHelper.KEY_AUTO_DATA)){ + if (umi.getSP(SharedHelper.KEY_AUTO_DATA) && !needUpdateHint){ + umi.makeToast(R.string.settings_auto_data); mainViewModel.checkDataUpdate(); } } diff --git a/app/src/main/java/com/lazyeraser/imas/cgss/viewmodel/LiveVm.java b/app/src/main/java/com/lazyeraser/imas/cgss/viewmodel/LiveVm.java index 6aba721..d74e153 100644 --- a/app/src/main/java/com/lazyeraser/imas/cgss/viewmodel/LiveVm.java +++ b/app/src/main/java/com/lazyeraser/imas/cgss/viewmodel/LiveVm.java @@ -7,16 +7,26 @@ import com.kelin.mvvmlight.command.ReplyCommand; import com.lazyeraser.imas.cgss.entity.LiveDetail; +import com.lazyeraser.imas.cgss.entity.Manifest; import com.lazyeraser.imas.cgss.entity.Note; import com.lazyeraser.imas.cgss.entity.SongRaw; +import com.lazyeraser.imas.cgss.service.CGSSService; import com.lazyeraser.imas.cgss.utils.DBHelper; +import com.lazyeraser.imas.cgss.utils.FileHelper; +import com.lazyeraser.imas.cgss.utils.LZ4Helper; +import com.lazyeraser.imas.cgss.utils.SharedHelper; import com.lazyeraser.imas.cgss.view.BeatMapActivity; +import com.lazyeraser.imas.derehelper.R; import com.lazyeraser.imas.main.BaseActivity; import com.lazyeraser.imas.main.BaseViewModel; +import com.lazyeraser.imas.retrofit.ExceptionHandler; +import com.lazyeraser.imas.retrofit.RetrofitProvider; +import java.io.IOException; import java.util.ArrayList; import java.util.List; +import okhttp3.ResponseBody; import rx.Observable; import rx.android.schedulers.AndroidSchedulers; import rx.schedulers.Schedulers; @@ -68,14 +78,54 @@ private void loadBeatMapData(int liveId, int diffcult){ .observeOn(AndroidSchedulers.mainThread()) .subscribe(o -> { SongRaw song = (SongRaw)o; - String value = new String(song.data); - Bundle bundle = new Bundle(); - bundle.putString("data", value); - bundle.putString("name", difficultyName.get()); - umi.jumpTo(BeatMapActivity.class, bundle); - }); + if (song != null){ + String value = new String(song.data); + Bundle bundle = new Bundle(); + bundle.putString("data", value); + bundle.putString("name", difficultyName.get()); + umi.dismissLoading(); + umi.jumpTo(BeatMapActivity.class, bundle); + }else { + updateBeatMapFile(liveId, diffcult); + } + }, throwable -> updateBeatMapFile(liveId, diffcult)); } catch (Exception e) { e.printStackTrace(); + updateBeatMapFile(liveId, diffcult); } } + + @SuppressLint("DefaultLocale") + private void updateBeatMapFile(int liveId, int diffcult){ + umi.showLoading(); + umi.makeToast(R.string.update_hint4); + String dbFile = String.format("/musicscores/musicscores_m%03d.bdb", liveId); + String fileName = String.format("musicscores_m%03d.bdb", liveId); + try { + Observable.just(DBHelper.with(mContext, DBHelper.DB_NAME_manifest) + .getBean(DBHelper.CGSS_TABLE_NAME_Manifest, Manifest.class, + "name", fileName)) + .subscribeOn(Schedulers.io()) + .subscribe(o -> { + Manifest manifest = (Manifest)o; + Observable file = umi.getSP(SharedHelper.KEY_USE_REVERSE_PROXY) ? + RetrofitProvider.getInstance(false).create(CGSSService.class).getResourcesRP(manifest.getHash()) : + RetrofitProvider.getInstance(false).create(CGSSService.class).getResources(manifest.getHash()); + file.subscribeOn(Schedulers.io()) + .subscribe(responseBody -> { + try { + FileHelper.writeFile(LZ4Helper.uncompressCGSS(responseBody.bytes()), mContext.getFilesDir().getAbsolutePath() + "/musicscores", fileName); + DBHelper.refresh(mContext, dbFile); + loadBeatMapData(liveId, diffcult); + } catch (IOException e) { + e.printStackTrace(); + } + }, ExceptionHandler::handleException); + + }, ExceptionHandler::handleException); + } catch (Exception e) { + e.printStackTrace(); + } + + } } diff --git a/app/src/main/java/com/lazyeraser/imas/cgss/viewmodel/MainViewModel.java b/app/src/main/java/com/lazyeraser/imas/cgss/viewmodel/MainViewModel.java index 04d4e65..bad069d 100644 --- a/app/src/main/java/com/lazyeraser/imas/cgss/viewmodel/MainViewModel.java +++ b/app/src/main/java/com/lazyeraser/imas/cgss/viewmodel/MainViewModel.java @@ -127,7 +127,7 @@ private void checkData() { boolean update = false; if (info != null) { String nowTruthVersion = umi.spRead(SharedHelper.KEY_TruthVersion); - if (!nowTruthVersion.equals(info.getTruth_version())) { + if (!nowTruthVersion.equals(info.getTruth_version()) || !FileHelper.isFileExists(mContext.getFilesDir().getAbsolutePath(), DBHelper.DB_NAME_master)) { // truth version differs, update manifest.db update = true; } else { @@ -177,8 +177,8 @@ private void updateManifest(String truthVersion) { manifestFile.subscribeOn(Schedulers.io()).subscribe(body -> { try { - FileHelper.writeFile(LZ4Helper.uncompressCGSS(body.bytes()), - mContext.getFilesDir().getAbsolutePath(), DBHelper.DB_NAME_manifest); + String mFilePath = mContext.getFilesDir().getAbsolutePath(); + FileHelper.writeFile(LZ4Helper.uncompressCGSS(body.bytes()), mFilePath, DBHelper.DB_NAME_manifest); // get hash of master.db List list = DBHelper.with(mContext, DBHelper.DB_NAME_manifest) .getBeanList(DBHelper.CGSS_TABLE_NAME_Manifest, Manifest.class, @@ -188,7 +188,7 @@ private void updateManifest(String truthVersion) { fileToDownload = new HashMap<>(); hashToDownload = new ArrayList<>(); - if (!masterHash.equals(umi.spRead(SharedHelper.KEY_MasterDbHash))) { + if (!masterHash.equals(umi.spRead(SharedHelper.KEY_MasterDbHash)) || !FileHelper.isFileExists(mFilePath, DBHelper.DB_NAME_master)) { // update master.db addFileDownloadMission(masterHash, DBHelper.DB_NAME_master, mContext.getFilesDir().getAbsolutePath()); } @@ -196,7 +196,7 @@ private void updateManifest(String truthVersion) { List musicList = DBHelper.with(mContext, DBHelper.DB_NAME_manifest) .getBeanListLike(DBHelper.CGSS_TABLE_NAME_Manifest, Manifest.class, "name", "%musicscores_%.bdb"); - String musicDataPath = mContext.getFilesDir().getAbsolutePath() + "/musicscores"; + String musicDataPath = mFilePath + "/musicscores"; for (Manifest manifest : musicList) { if (!FileHelper.isFileExists(musicDataPath, manifest.getName())) { addFileDownloadMission(manifest.getHash(), manifest.getName(), musicDataPath); @@ -243,10 +243,13 @@ private void downLoadFiles(int i, String masterHash, String truthVersion){ e.printStackTrace(); } }, throwable -> { - if (retryTimes < 50){ + if (retryTimes < 10){ if (throwable instanceof HttpException || throwable instanceof SocketTimeoutException || throwable instanceof ConnectException){ downLoadFiles(i, masterHash, truthVersion); + retryTimes ++; + }else { + umi.makeToast(R.string.network_error_0); } }else { umi.makeToast(R.string.network_error_0); @@ -264,9 +267,7 @@ private void finishManifestUpdate(String masterHash, String truthVersion){ progress.set(1); progressTxt.set("100%"); // update masterHash - if (!masterHash.equals(umi.spRead(SharedHelper.KEY_MasterDbHash))) { - umi.spSave(SharedHelper.KEY_MasterDbHash, masterHash); - } + umi.spSave(SharedHelper.KEY_MasterDbHash, masterHash); // update truth version umi.spSave(SharedHelper.KEY_TruthVersion, truthVersion); }); diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index 32a5143..a677bc9 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -24,7 +24,7 @@ 要现在更新吗(建议在wifi环境下进行) 下次再说 立即更新 - 千寻记录中 + 下载中 写入异常,请尝试重启APP进行重试 缓存中 更新完成 diff --git a/app/src/main/res/values-zh-rHK/strings.xml b/app/src/main/res/values-zh-rHK/strings.xml index 5e8f5bb..243aa7a 100644 --- a/app/src/main/res/values-zh-rHK/strings.xml +++ b/app/src/main/res/values-zh-rHK/strings.xml @@ -24,7 +24,7 @@ 要現在更新嗎(建議在wifi環境下進行) 下次再說 立即更新 - 千尋記錄中 + 下載中 寫入異常,請嘗試重啟APP進行重試 緩存中 更新完成 diff --git a/app/src/main/res/values-zh-rTW/strings.xml b/app/src/main/res/values-zh-rTW/strings.xml index 2d553cc..309f0fd 100644 --- a/app/src/main/res/values-zh-rTW/strings.xml +++ b/app/src/main/res/values-zh-rTW/strings.xml @@ -24,7 +24,7 @@ 要現在更新嗎(建議在wifi環境下進行) 下次再說 立即更新 - 千尋記錄中 + 下載中 寫入異常,請嘗試重啟APP進行重試 緩存中 更新完成