Skip to content

Commit

Permalink
1.9
Browse files Browse the repository at this point in the history
  • Loading branch information
jsbxyyx committed Jul 18, 2024
1 parent 2d9d94b commit fa887c6
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 5 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ android {
applicationId "com.github.jsbxyyx.xbook"
minSdk 27
targetSdk 33
versionCode 18
versionName "1.8"
versionCode 19
versionName "1.9"
archivesBaseName = "xplay-v${versionName}"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand Down
73 changes: 73 additions & 0 deletions app/src/main/java/com/github/jsbxyyx/xbook/HomeFragment.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
package com.github.jsbxyyx.xbook;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.fasterxml.jackson.databind.JsonNode;
import com.github.jsbxyyx.xbook.common.DataCallback;
import com.github.jsbxyyx.xbook.common.LogUtil;
import com.github.jsbxyyx.xbook.data.BookNetHelper;

/**
* @author jsbxyyx
* @since 1.0
Expand All @@ -17,11 +29,72 @@ public class HomeFragment extends Fragment {

private String TAG = "xbook";

private View mView;
private Activity mActivity;

private BookNetHelper bookNetHelper;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
bookNetHelper = new BookNetHelper();
return view;
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

mView = view;
mActivity = getActivity();

bookNetHelper.cloudVersions(new DataCallback<JsonNode>() {
@Override
public void call(JsonNode o, Throwable err) {
if (err != null) {
LogUtil.d(getClass().getSimpleName(), "%s", LogUtil.getStackTraceString(err));
mActivity.runOnUiThread(() -> {
Toast.makeText(mActivity, "获取版本更新失败:" + err.getMessage(), Toast.LENGTH_LONG).show();
});
return;
}
JsonNode data = o.get("data");
if (data.isEmpty()) {
LogUtil.d(getClass().getSimpleName(), "versions empty.");
return;
}
try {
JsonNode update = data.get(0);
PackageInfo packageInfo = mActivity.getPackageManager().getPackageInfo(mActivity.getPackageName(), 0);
double localName = Double.parseDouble(packageInfo.versionName);
double cloudName = Double.parseDouble(update.get("name").asText().trim());
if (cloudName > localName) {
mActivity.runOnUiThread(() -> {
new AlertDialog.Builder(mActivity)
.setTitle("提示")
.setMessage("有新版本啦,前往 我的-设置 进行版本更新")
.setIcon(android.R.drawable.ic_dialog_alert)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Intent localIntent = new Intent(mActivity, SettingsActivity.class);
startActivity(localIntent);
}
}).setNegativeButton(android.R.string.no, null)
.setCancelable(false)
.show();
});
}
} catch (PackageManager.NameNotFoundException e) {
LogUtil.e(TAG, "获取版本失败");
mActivity.runOnUiThread(() -> {
Toast.makeText(mActivity, "获取版本失败", Toast.LENGTH_LONG).show();
});
return;
}
}
});

}

}
2 changes: 0 additions & 2 deletions app/src/main/java/com/github/jsbxyyx/xbook/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@ public void onClick(DialogInterface dialog, int whichButton) {
.show();
}



bottomNavigationView = findViewById(R.id.bottom_navigation);

fm = getSupportFragmentManager();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ protected void onCreate(Bundle savedInstanceState) {
PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
tv_version.setText(packageInfo.versionName);
} catch (PackageManager.NameNotFoundException e) {
throw new RuntimeException(e);
LogUtil.e(getClass().getSimpleName(), "获取版本失败", e);
Toast.makeText(this, "获取版本失败", Toast.LENGTH_LONG).show();
}

Button btn_update = findViewById(R.id.btn_update);
Expand All @@ -93,6 +94,10 @@ public void call(File file, Throwable err) {
return;
}
LogUtil.d(getClass().getSimpleName(), "下载成功,开始安装");
runOnUiThread(() -> {
Toast.makeText(context, "下载成功,开始安装", Toast.LENGTH_LONG).show();
});
Common.sleep(1000);
Intent install = new Intent(Intent.ACTION_VIEW);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
install.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/java/com/github/jsbxyyx/xbook/common/Common.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,10 @@ public static boolean isBlank(String str) {
return str == null || str.trim().isEmpty();
}

public static void sleep(int millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException ignore) {
}
}
}

0 comments on commit fa887c6

Please sign in to comment.