Skip to content

Commit

Permalink
1.0.2 : fix crash in beatmap
Browse files Browse the repository at this point in the history
  • Loading branch information
wxwang committed Jan 27, 2018
1 parent b009c96 commit 161689d
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 34 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
28 changes: 20 additions & 8 deletions app/src/main/java/com/lazyeraser/imas/cgss/utils/DBHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -104,6 +114,7 @@ public static DBHelper getInstance(String dbName){
/*====================================*/



private DBHelper(Context context, String dbName) {
super(context, dbName, null, DB_VERSION);
onCreate(getReadableDatabase());
Expand All @@ -112,15 +123,16 @@ private DBHelper(Context context, String dbName) {

@Override
public void onCreate(SQLiteDatabase db) {
List<String> 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<String> 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(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -52,7 +52,6 @@ public static boolean writeFile(byte[] file, String filePath, String fileName) {
e.printStackTrace();
}
}
Utils.mPrint("写入文件成功" + filePath + "/" + fileName);
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
// 监听语言设置改变
Expand Down Expand Up @@ -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();
}
}
Expand All @@ -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();
}
}
Expand Down
62 changes: 56 additions & 6 deletions app/src/main/java/com/lazyeraser/imas/cgss/viewmodel/LiveVm.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<ResponseBody> 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();
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<Manifest> list = DBHelper.with(mContext, DBHelper.DB_NAME_manifest)
.getBeanList(DBHelper.CGSS_TABLE_NAME_Manifest, Manifest.class,
Expand All @@ -188,15 +188,15 @@ 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());
}
// update music (beatMap
List<Manifest> 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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
});
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<string name="update_hint1">要现在更新吗(建议在wifi环境下进行)</string>
<string name="update_hint2">下次再说</string>
<string name="update_hint3">立即更新</string>
<string name="update_hint4">千寻记录中</string>
<string name="update_hint4">下载中</string>
<string name="update_error">写入异常,请尝试重启APP进行重试</string>
<string name="update_hint5">缓存中</string>
<string name="update_hint_finish">更新完成</string>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-zh-rHK/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<string name="update_hint1">要現在更新嗎(建議在wifi環境下進行)</string>
<string name="update_hint2">下次再說</string>
<string name="update_hint3">立即更新</string>
<string name="update_hint4">千尋記錄中</string>
<string name="update_hint4">下載中</string>
<string name="update_error">寫入異常,請嘗試重啟APP進行重試</string>
<string name="update_hint5">緩存中</string>
<string name="update_hint_finish">更新完成</string>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-zh-rTW/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<string name="update_hint1">要現在更新嗎(建議在wifi環境下進行)</string>
<string name="update_hint2">下次再說</string>
<string name="update_hint3">立即更新</string>
<string name="update_hint4">千尋記錄中</string>
<string name="update_hint4">下載中</string>
<string name="update_error">寫入異常,請嘗試重啟APP進行重試</string>
<string name="update_hint5">緩存中</string>
<string name="update_hint_finish">更新完成</string>
Expand Down

0 comments on commit 161689d

Please sign in to comment.