Skip to content

Commit

Permalink
快速修改,UI优化
Browse files Browse the repository at this point in the history
  • Loading branch information
xz-dev committed May 10, 2019
1 parent afbcdc0 commit 216694d
Show file tree
Hide file tree
Showing 20 changed files with 387 additions and 117 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ android {
applicationId "net.xzos.upgradeall"
minSdkVersion 21
targetSdkVersion 28
versionCode 6
versionName "0.0.5"
versionCode 8
versionName "0.0.6"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
53 changes: 29 additions & 24 deletions app/src/main/java/net/xzos/UpgradeAll/HttpApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@

class HttpApi {

public boolean flashData() {
private static final String TAG = "HttpApi";

public void flashData() {
}

public boolean isSuccessFlash() {
return false;
}

Expand Down Expand Up @@ -57,7 +62,7 @@ static String getHttpResponse(String api_url) {
try {
response = client.newCall(request).execute();
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, "getHttpResponse: 网络错误");
}
if (response != null) {
try {
Expand All @@ -72,7 +77,7 @@ static String getHttpResponse(String api_url) {


class GithubApi extends HttpApi {
private JSONArray returnJsonArray;
private JSONArray returnJsonArray = new JSONArray();
private String api_url;
private static final String TAG = "GithubApi";

Expand All @@ -82,37 +87,33 @@ class GithubApi extends HttpApi {
flashData();
}

public boolean flashData() {
public void flashData() {
// 仅刷新数据,并进行数据校验
boolean refreshSuccess = false;
if (api_url.length() != 0) {
String jsonText = getHttpResponse(api_url);
// 如果刷新失败,则不记录数据
if (jsonText.length() != 0) {
JSONArray returnJsonArray = new JSONArray();
try {
returnJsonArray = new JSONArray(jsonText);
this.returnJsonArray = new JSONArray(jsonText);
} catch (JSONException e) {
e.printStackTrace();
}
if (returnJsonArray.length() != 0) {
this.returnJsonArray = returnJsonArray;
refreshSuccess = true;
// 数据获取成功,返回 true
}
}
}
Log.d(TAG, "getRelease: returnJsonArray: " + returnJsonArray);
return refreshSuccess;
}

public boolean isSuccessFlash() {
// 数据获取成功,返回 true
return this.returnJsonArray.length() != 0;
}

private JSONObject getRelease(int releaseNum) {
JSONObject jsonObject = new JSONObject();
try {
jsonObject = new JSONObject(this.returnJsonArray.getString(releaseNum));
} catch (NullPointerException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
Log.e(TAG, String.format("getRelease: 返回数据解析错误: %s, returnJsonArray: %s", releaseNum, this.returnJsonArray));
}
return jsonObject;
}
Expand All @@ -129,7 +130,7 @@ String getVersion(int releaseNum) {
try {
latestVersion = getRelease(releaseNum).getString("name");
} catch (JSONException e) {
e.printStackTrace();
Log.e(TAG, String.format("getVersion: 返回数据解析错误: %s, returnJsonArray: %s", "name", getRelease(releaseNum)));
}
return latestVersion;
}
Expand All @@ -141,7 +142,7 @@ JSONObject getReleaseDownloadUrl(int releaseNum) {
try {
releaseAssets = getRelease(releaseNum).getJSONArray("assets");
} catch (JSONException e) {
e.printStackTrace();
Log.e(TAG, String.format(" getReleaseDownloadUrl: 返回数据解析错误: %s, returnJsonArray: %s", "assets", getRelease(releaseNum)));
}
for (int i = 0; i < releaseAssets.length(); i++) {
JSONObject tmpJsonObject = new JSONObject();
Expand All @@ -166,11 +167,15 @@ static String[] getApiUrl(String url) {
temp = temp[temp.length - 1].split("/");
List<String> list = new ArrayList<>(Arrays.asList(temp));
list.removeAll(Arrays.asList("", null));
String owner = list.get(0);
String repo = list.get(1);
// 分割网址
String apiUrl = "https://api.github.com/repos/"
+ owner + "/" + repo + "/releases";
return new String[]{apiUrl, repo};
if (list.size() == 2) {
String owner = list.get(0);
String repo = list.get(1);
// 分割网址
String apiUrl = "https://api.github.com/repos/"
+ owner + "/" + repo + "/releases";
return new String[]{apiUrl, repo};
} else {
return null;
}
}
}
4 changes: 1 addition & 3 deletions app/src/main/java/net/xzos/UpgradeAll/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,7 @@ protected void onCreate(Bundle savedInstanceState) {

private void refreshUpdate() {
new Thread(() -> {
runOnUiThread(() -> {
swipeRefresh.setRefreshing(true);
});
runOnUiThread(() -> swipeRefresh.setRefreshing(true));
updater.refreshAll(true);
runOnUiThread(() -> {
refreshCardView();
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/net/xzos/UpgradeAll/RepoDatabase.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package net.xzos.UpgradeAll;

import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;
import org.litepal.crud.LitePalSupport;

public class RepoDatabase extends LitePalSupport {
private static final String TAG = "RepoDatabase";
private int id;
private String name;
private String api;
Expand Down Expand Up @@ -55,6 +58,7 @@ void setApiUrl(String api_url) {

JSONObject getVersionChecker() {
try {
Log.d(TAG, "getVersionChecker: " + versionChecker);
return new JSONObject(versionChecker);
} catch (JSONException e) {
e.printStackTrace();
Expand Down
112 changes: 81 additions & 31 deletions app/src/main/java/net/xzos/UpgradeAll/UpdateItemCardAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
Expand All @@ -32,6 +34,8 @@ public class UpdateItemCardAdapter extends RecyclerView.Adapter<UpdateItemCardAd

private List<UpdateCard> mUpdateList;

final private Updater updater = MyApplication.getUpdater();

UpdateItemCardAdapter(List<UpdateCard> updateList) {
mUpdateList = updateList;
}
Expand All @@ -43,7 +47,8 @@ static class ViewHolder extends RecyclerView.ViewHolder {
TextView api;
ProgressBar versionCheckingBar;
ImageView versionCheckButton;
CardView del_button;
CardView delButton;
CardView settingButton;
RecyclerView updateItemCardList;

ViewHolder(View view) {
Expand All @@ -54,7 +59,8 @@ static class ViewHolder extends RecyclerView.ViewHolder {
api = view.findViewById(R.id.apiTextView);
versionCheckingBar = view.findViewById(R.id.versionCheckingBar);
versionCheckButton = view.findViewById(R.id.versionCheckTextButton);
del_button = view.findViewById(R.id.del_button);
delButton = view.findViewById(R.id.del_button);
settingButton = view.findViewById(R.id.setting_button);
updateItemCardList = view.findViewById(R.id.item_list_view);
}
}
Expand All @@ -69,35 +75,56 @@ public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
UpdateCard updateCard = mUpdateList.get(position);
int databaseId = updateCard.getDatabaseId();
holder.name.setText(updateCard.getName());
holder.api.setText(updateCard.getApi());
holder.url.setText(updateCard.getUrl());
refreshUpdater(true, holder, updateCard);
refreshUpdater(true, databaseId, holder);

// 单击展开 Release 详情页
holder.versionCheckButton.setOnClickListener(v -> {
// 单击展开 Release 详情页
JSONObject latestDownloadUrl = MyApplication.getUpdater().getLatestDownloadUrl(updateCard.getDatabaseId());
AlertDialog.Builder builder = new AlertDialog.Builder(holder.versionCheckingBar.getContext());
final AlertDialog dialog = builder.setView(R.layout.dialog_version).create();
dialog.show();

// 显示本地版本号
TextView cloudReleaseTextView = dialog.getWindow().findViewById(R.id.cloud_release_text_view);
String latestVersion = updater.getLatestVersion(databaseId);
if (latestVersion != null)
cloudReleaseTextView.setText(latestVersion);
else
cloudReleaseTextView.setText("获取失败");
TextView localReleaseTextView = dialog.getWindow().findViewById(R.id.local_release_text_view);
String installedVersion = updater.getInstalledVersion(databaseId);
if (installedVersion != null)
localReleaseTextView.setText(installedVersion);
else
localReleaseTextView.setText("获取失败");

// 获取云端文件
JSONObject latestDownloadUrl = updater.getLatestDownloadUrl(databaseId);
List<String> itemList = new ArrayList<>();
Iterator<String> sIterator = latestDownloadUrl.keys();
while (sIterator.hasNext()) {
String key = sIterator.next();
itemList.add(key);
}
holder.versionCheckButton.setOnLongClickListener(v1 -> {
// 长按强制检查版本
refreshUpdater(false, holder, updateCard);
return true;
});
String[] itemStringArray = itemList.toArray(new String[0]);
// 获取文件列表

AlertDialog.Builder builder = new AlertDialog.Builder(holder.versionCheckingBar.getContext());
builder.setItems(itemStringArray, (dialog, which) ->
// 无Release文件,不显示网络文件列表
if (itemList.size() == 0) {
dialog.getWindow().findViewById(R.id.releaseTextView).setVisibility(View.INVISIBLE);
}

{
// 构建文件列表
ArrayAdapter<String> adapter = new ArrayAdapter<>(
dialog.getContext(), android.R.layout.simple_list_item_1, itemList);
ListView cloudReleaseList = dialog.getWindow().findViewById(R.id.cloudReleaseList);
// 设置文件列表点击事件
cloudReleaseList.setOnItemClickListener((parent, view, position1, id) -> {
Intent intent = new Intent(Intent.ACTION_VIEW);
String url = null;
try {
url = latestDownloadUrl.getString(itemList.get(which));
url = latestDownloadUrl.getString(itemList.get(position1));
} catch (JSONException e) {
e.printStackTrace();
}
Expand All @@ -106,8 +133,15 @@ public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
MyApplication.getContext().startActivity(intent);
});
builder.show();
cloudReleaseList.setAdapter(adapter);
});

// 长按强制检查版本
holder.versionCheckButton.setOnLongClickListener(v1 -> {
refreshUpdater(false, databaseId, holder);
return true;
});

// 打开指向Url
holder.url.setOnClickListener(v -> {
Intent intent = new Intent(Intent.ACTION_VIEW);
Expand All @@ -116,41 +150,57 @@ public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
MyApplication.getContext().startActivity(intent);
});
// 修改按钮
holder.settingButton.setOnClickListener(v -> {
Intent intent = new Intent(holder.settingButton.getContext(), UpdateItemSettingActivity.class);
intent.putExtra("database_id", databaseId);
holder.settingButton.getContext().startActivity(intent);
});
// 删除按钮
holder.del_button.setOnClickListener(v -> {
holder.delButton.setOnClickListener(v -> {
// 删除数据库
String api_url = GithubApi.getApiUrl(holder.url.getText().toString())[0];
LitePal.deleteAll(RepoDatabase.class, "api_url = ?", api_url);
LitePal.delete(RepoDatabase.class, databaseId);
// 删除指定数据库
Intent intent = new Intent(holder.del_button.getContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
MyApplication.getContext().startActivity(intent);
Intent intent = new Intent(holder.delButton.getContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
holder.delButton.getContext().startActivity(intent);
// 重新跳转刷新界面
// TODO: 需要优化刷新方法
});
}

private void refreshUpdater(boolean isAuto, ViewHolder holder, UpdateCard updateCard) {
private void refreshUpdater(boolean isAuto, int databaseId, ViewHolder holder) {
if (!isAuto) {
Toast.makeText(holder.versionCheckButton.getContext(), String.format("检查 %s 的更新", holder.name.getText().toString()),
Toast.LENGTH_SHORT).show();
}
holder.versionCheckButton.setVisibility(View.INVISIBLE);
holder.versionCheckingBar.setVisibility(View.VISIBLE);
new Thread(() -> {
int databaseId = updateCard.getDatabaseId();
if (isAuto) {
MyApplication.getUpdater().autoRefresh(databaseId);
updater.autoRefresh(databaseId);
} else {
MyApplication.getUpdater().refresh(databaseId);
updater.refresh(databaseId);
}
// 刷新数据库
new Handler(Looper.getMainLooper()).post(() -> {
holder.versionCheckButton.setVisibility(View.VISIBLE);
holder.versionCheckingBar.setVisibility(View.INVISIBLE);
Updater updater = MyApplication.getUpdater();
if (updater.isLatest(databaseId)) {
holder.versionCheckButton.setImageResource(R.drawable.ic_check_latest);
//检查是否取得云端版本号
if (updater.getLatestVersion(databaseId) != null) {
// 检查是否获取本地版本号
if (updater.getInstalledVersion(databaseId) != null) {
// 检查本地版本
if (updater.isLatest(databaseId)) {
holder.versionCheckButton.setImageResource(R.drawable.ic_check_latest);
} else {
holder.versionCheckButton.setImageResource(R.drawable.ic_check_needupdate);
}
} else {
holder.versionCheckButton.setImageResource(R.drawable.ic_local_error);
}
} else {
holder.versionCheckButton.setImageResource(R.drawable.ic_check_needupdate);
holder.versionCheckButton.setImageResource(R.drawable.ic_404);
}
});
}).start();
Expand Down
Loading

0 comments on commit 216694d

Please sign in to comment.