Skip to content

Commit e281f02

Browse files
committed
FIX: 从GitHub更新的功能
TODO: 自动关闭SSR 看看能不能直接从B站下资源?
1 parent d696858 commit e281f02

File tree

4 files changed

+58
-18
lines changed

4 files changed

+58
-18
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
77
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
88
<uses-permission android:name="android.permission.INTERNET" />
9+
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
910

1011
<application
1112
android:allowBackup="false"

app/src/main/java/com/brainor/bilihelper/AboutSettingsActivity.java

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
package com.brainor.bilihelper;
22

3+
import android.app.DownloadManager;
4+
import android.content.BroadcastReceiver;
5+
import android.content.Context;
36
import android.content.Intent;
7+
import android.content.IntentFilter;
8+
import android.content.pm.PackageManager;
49
import android.os.AsyncTask;
510
import android.os.Bundle;
611
import android.text.Html;
712
import android.text.Spanned;
813
import android.text.method.LinkMovementMethod;
14+
import android.util.Log;
915
import android.view.View;
16+
import android.widget.Button;
1017
import android.widget.TextView;
1118
import android.widget.Toast;
1219

@@ -27,6 +34,16 @@
2734
public class AboutSettingsActivity extends AppCompatActivity {
2835
static private String downUrl;
2936
static private String latestVersion;
37+
Button updateButton;
38+
Long extra_download_id = 0L;
39+
BroadcastReceiver downloadComplete = new BroadcastReceiver() {
40+
@Override
41+
public void onReceive(Context context, Intent intent) {
42+
if (intent.getLongExtra("extra_download_id", 0) == extra_download_id) {
43+
updateButton.callOnClick();
44+
}
45+
}
46+
};
3047

3148
@Override
3249
protected void onCreate(Bundle savedInstanceState) {
@@ -36,19 +53,30 @@ protected void onCreate(Bundle savedInstanceState) {
3653
setSupportActionBar(toolbar);
3754
Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
3855

39-
findViewById(R.id.updateButton).setOnClickListener(v -> {
56+
String verText = "版本号 " + BuildConfig.VERSION_NAME;
57+
((TextView) findViewById(R.id.version)).setText(verText);
58+
updateButton = findViewById(R.id.updateButton);
59+
updateButton.setOnClickListener(v -> {
4060
File appFile = new File(Settings.downloadAPKPath, "Bilihelper-" + latestVersion + ".apk");
4161
if (appFile.exists())//若存在则安装
42-
startActivityForResult(new Intent(Intent.ACTION_INSTALL_PACKAGE)
62+
startActivity(new Intent(Intent.ACTION_INSTALL_PACKAGE)
4363
.setData(FileProvider.getUriForFile(AboutSettingsActivity.this, getApplicationContext().getPackageName() + ".fileprovider", appFile))
44-
.putExtra(Intent.EXTRA_RETURN_RESULT, true)
45-
.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true)
46-
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION), 144);
64+
.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION));
4765
else {//若不存在则下载
48-
MainActivity.DownloadTask(downUrl, Settings.downloadAPKPath + "BiliHelper-" + latestVersion + ".apk", "BiliHelper-" + latestVersion + ".apk", AboutSettingsActivity.this);
66+
extra_download_id = MainActivity.DownloadTask(downUrl, Settings.downloadAPKPath + "BiliHelper-" + latestVersion + ".apk", "BiliHelper-" + latestVersion + ".apk", AboutSettingsActivity.this);
4967
Toast.makeText(AboutSettingsActivity.this, "下载Bilihelper.apk", Toast.LENGTH_SHORT).show();
5068
}
5169
});
70+
71+
updateButton.setOnLongClickListener(v -> {
72+
File appFile = new File(Settings.downloadAPKPath, "Bilihelper-" + latestVersion + ".apk");
73+
if (appFile.exists())//若存在则删除
74+
if (appFile.delete())
75+
Toast.makeText(AboutSettingsActivity.this, "文件已删除", Toast.LENGTH_SHORT).show();
76+
else Toast.makeText(AboutSettingsActivity.this, "文件未删除", Toast.LENGTH_SHORT).show();
77+
78+
return true;
79+
});
5280
new getLatestVersion().execute();
5381

5482
Spanned styledText = Html.fromHtml("<h1><a id=\"_0\"></a>开发</h1>\n" +
@@ -78,6 +106,8 @@ protected void onCreate(Bundle savedInstanceState) {
78106
TextView aboutTextView = findViewById(R.id.aboutText);
79107
aboutTextView.setText(styledText);
80108
aboutTextView.setMovementMethod(LinkMovementMethod.getInstance());
109+
110+
81111
}
82112

83113
class getLatestVersion extends AsyncTask<Void, Void, String> {
@@ -101,6 +131,7 @@ protected void onCancelled(String ErrMessage) {
101131

102132
@Override
103133
protected void onPostExecute(String html) {
134+
updateButton.setVisibility(View.VISIBLE);
104135
latestVersion = BuildConfig.VERSION_NAME;
105136
try {
106137
JSONObject json = new JSONObject(html);
@@ -109,16 +140,23 @@ protected void onPostExecute(String html) {
109140
} catch (JSONException e) {
110141
Toast.makeText(AboutSettingsActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
111142
}
112-
String verText;
113143
if (!Objects.equals(BuildConfig.VERSION_NAME, latestVersion)) {
114-
verText = "版本号 " + BuildConfig.VERSION_NAME + "\n最新版本 " + latestVersion;
115-
((TextView) findViewById(R.id.version)).setText(verText);
116-
findViewById(R.id.updateButton).setVisibility(View.VISIBLE);
117-
} else {
118-
verText = "版本号 " + latestVersion;
119-
((TextView) findViewById(R.id.version)).setText(verText);
120-
findViewById(R.id.updateButton).setVisibility(View.GONE);
121-
}
144+
String updateText = "更新至" + latestVersion;
145+
updateButton.setText(updateText);
146+
} else updateButton.setText("更新");
147+
122148
}
123149
}
150+
151+
@Override
152+
protected void onResume() {
153+
super.onResume();
154+
registerReceiver(downloadComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
155+
}
156+
157+
@Override
158+
protected void onPause() {
159+
super.onPause();
160+
unregisterReceiver(downloadComplete);
161+
}
124162
}

app/src/main/java/com/brainor/bilihelper/MainActivity.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ static void StoreHistory(Context context) {
315315
sharedPref.edit().putStringSet("HistoryList", HistoryStrSet).apply();
316316
}
317317

318-
static void DownloadTask(String url, String filePath, String title,Context context) {
318+
static Long DownloadTask(String url, String filePath, String title,Context context) {
319319
//关闭VPN
320320
DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
321321
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
@@ -326,7 +326,7 @@ static void DownloadTask(String url, String filePath, String title,Context conte
326326
//MIUI必须关闭迅雷下载引擎
327327
request.setTitle(title);
328328
request.setDestinationUri(Uri.fromFile(new File(filePath)));
329-
downloadManager.enqueue(request);
329+
return downloadManager.enqueue(request);
330330
}
331331

332332
@Override
@@ -348,4 +348,5 @@ protected void onResume() {
348348
Intent intent = getIntent();
349349
onNewIntent(intent);
350350
}
351+
351352
}

app/src/main/res/layout/about_settings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@
5353
android:focusable="true"
5454
android:minHeight="0dp"
5555
android:padding="8dp"
56-
android:text="更新"
5756
android:textSize="14sp"
57+
android:visibility="invisible"
5858
app:layout_constraintEnd_toEndOf="parent"
5959
app:layout_constraintStart_toStartOf="parent"
6060
app:layout_constraintTop_toBottomOf="@+id/version" />

0 commit comments

Comments
 (0)