Skip to content

Commit 35ece25

Browse files
committed
Update: 从GitHub中获取更新
TODO: 自动关闭SSR 看看能不能直接从B站下资源?
1 parent 63fd72c commit 35ece25

File tree

7 files changed

+147
-30
lines changed

7 files changed

+147
-30
lines changed

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/src/main/AndroidManifest.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@
4747
android:name=".Settings"
4848
android:label="@string/action_settings"
4949
android:parentActivityName=".MainActivity" />
50+
<provider
51+
android:authorities="${applicationId}.fileprovider"
52+
android:name="androidx.core.content.FileProvider"
53+
android:exported="false"
54+
android:grantUriPermissions="true">
55+
<meta-data
56+
android:name="android.support.FILE_PROVIDER_PATHS"
57+
android:resource="@xml/file_paths"/>
58+
</provider>
5059
</application>
5160

5261
</manifest>

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

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,32 @@
11
package com.brainor.bilihelper;
22

3+
import android.content.Intent;
4+
import android.os.AsyncTask;
35
import android.os.Bundle;
46
import android.text.Html;
57
import android.text.Spanned;
68
import android.text.method.LinkMovementMethod;
9+
import android.view.View;
710
import android.widget.TextView;
11+
import android.widget.Toast;
812

13+
import org.json.JSONException;
14+
import org.json.JSONObject;
15+
16+
import java.io.File;
17+
import java.io.IOException;
918
import java.util.Objects;
1019

1120
import androidx.appcompat.app.AppCompatActivity;
1221
import androidx.appcompat.widget.Toolbar;
22+
import androidx.core.content.FileProvider;
23+
import okhttp3.OkHttpClient;
24+
import okhttp3.Request;
25+
1326

1427
public class AboutSettingsActivity extends AppCompatActivity {
28+
static private String downUrl;
29+
static private String latestVersion;
1530

1631
@Override
1732
protected void onCreate(Bundle savedInstanceState) {
@@ -21,10 +36,22 @@ protected void onCreate(Bundle savedInstanceState) {
2136
setSupportActionBar(toolbar);
2237
Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
2338

24-
TextView aboutTextView = findViewById(R.id.aboutText);
39+
findViewById(R.id.updateButton).setOnClickListener(v -> {
40+
File appFile = new File(Settings.downloadAPKPath, "Bilihelper-" + latestVersion + ".apk");
41+
if (appFile.exists())//若存在则安装
42+
startActivityForResult(new Intent(Intent.ACTION_INSTALL_PACKAGE)
43+
.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);
47+
else {//若不存在则下载
48+
MainActivity.DownloadTask(downUrl, Settings.downloadAPKPath + "BiliHelper-" + latestVersion + ".apk", "BiliHelper-" + latestVersion + ".apk", AboutSettingsActivity.this);
49+
Toast.makeText(AboutSettingsActivity.this, "下载Bilihelper.apk", Toast.LENGTH_SHORT).show();
50+
}
51+
});
52+
new getLatestVersion().execute();
2553

26-
Spanned styledText = Html.fromHtml("<p style=\"text-align: center;\">版本号 " + BuildConfig.VERSION_NAME + "</p>\n" +
27-
"<h1><a id=\"_0\"></a>开发</h1>\n" +
54+
Spanned styledText = Html.fromHtml("<h1><a id=\"_0\"></a>开发</h1>\n" +
2855
"<p><a href=\"https://github.com/brainor\">@Brainor</a> 使用Java开发<br>\n" +
2956
"<a href=\"mailto:[email protected]\">[email protected]</a>.<br><br>\n" +
3057
"本程序主要参考<a href=\"https://github.com/xiaoyaocz/BiliAnimeDownload\">@xiaoyaocz</a>的Xamarin程序, 该Android版APP可在<a href=\"https://www.coolapk.com/apk/com.xiaoyaocz.bilidownload\">酷安</a>下载.<br>\n" +
@@ -40,15 +67,58 @@ protected void onCreate(Bundle savedInstanceState) {
4067
"</ol>\n" +
4168
"<h1><a id=\"_12\"></a>引用&amp;开源</h1>\n" +
4269
"<p>本程序使用了Biliplus的API, 具体可参考Biliplus的<a href=\"https://www.biliplus.com/api/README\">开放接口</a>.<br>\n" +
43-
"网络通讯 <a href=\"http://square.github.io/okhttp/\">square/okhttp</a>.</p>"+
70+
"网络通讯 <a href=\"http://square.github.io/okhttp/\">square/okhttp</a>.</p>" +
4471
"<h1><a id=\"_15\"></a>已知问题</h1>\n" +
4572
"<ol>\n" +
4673
"<li>可能BiliPlus也没有相关资源的下载链接</li>\n" +
4774
"<li>B站和BiliPlus提供的链接都是acgvideo.com域名下的, 需要特定的headers或者有IP限制, 会导致下载失败. 前者可以解决, 后者无法解决.</li>\n" +
4875
"<li>B站账户必须超过5级才可以看会员视频(Biliplus限制).</li>" +
4976
"<li><font color=\"red\"><b>目前Biliplus已挂.</b></font></li>" +
5077
"</ol>\n", Html.FROM_HTML_SEPARATOR_LINE_BREAK_LIST_ITEM);
78+
TextView aboutTextView = findViewById(R.id.aboutText);
5179
aboutTextView.setText(styledText);
5280
aboutTextView.setMovementMethod(LinkMovementMethod.getInstance());
5381
}
82+
83+
class getLatestVersion extends AsyncTask<Void, Void, String> {
84+
@Override
85+
protected String doInBackground(Void... voids) {
86+
try {//获得最新版本
87+
final OkHttpClient okHttpClient = new OkHttpClient();
88+
return Objects.requireNonNull(okHttpClient.newCall(new Request.Builder()
89+
.url("https://api.github.com/repos/Brainor/BiliHelper/releases/latest")
90+
.build()).execute().body()).string();
91+
} catch (IOException | NullPointerException e) {
92+
cancel(true);
93+
return e.getMessage();
94+
}
95+
}
96+
97+
@Override
98+
protected void onCancelled(String ErrMessage) {
99+
Toast.makeText(AboutSettingsActivity.this, ErrMessage, Toast.LENGTH_LONG).show();
100+
}
101+
102+
@Override
103+
protected void onPostExecute(String html) {
104+
latestVersion = BuildConfig.VERSION_NAME;
105+
try {
106+
JSONObject json = new JSONObject(html);
107+
latestVersion = json.getString("name").substring(1);//第一个字是"v"
108+
downUrl = json.getJSONArray("assets").getJSONObject(0).getString("browser_download_url");
109+
} catch (JSONException e) {
110+
Toast.makeText(AboutSettingsActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
111+
}
112+
String verText;
113+
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+
}
122+
}
123+
}
54124
}

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

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66
import android.content.Intent;
77
import android.content.SharedPreferences;
88
import android.content.pm.PackageManager;
9+
import android.graphics.Color;
910
import android.graphics.Point;
1011
import android.net.Uri;
1112
import android.os.AsyncTask;
1213
import android.os.Bundle;
1314
import android.view.Menu;
1415
import android.view.MenuItem;
16+
import android.view.View;
17+
import android.view.ViewGroup;
1518
import android.webkit.CookieManager;
1619
import android.webkit.WebView;
1720
import android.webkit.WebViewClient;
@@ -35,6 +38,7 @@
3538
import java.util.regex.Pattern;
3639
import java.util.stream.Stream;
3740

41+
import androidx.annotation.NonNull;
3842
import androidx.appcompat.app.AlertDialog;
3943
import androidx.appcompat.app.AppCompatActivity;
4044
import androidx.appcompat.widget.Toolbar;
@@ -59,7 +63,16 @@ protected void onCreate(Bundle savedInstanceState) {
5963
inputTextView = findViewById(R.id.inputText);
6064
// inputTextView.setText("ss25696");//调试用
6165
infoListView = findViewById(R.id.infoListView);
62-
infoListView.setAdapter(new ArrayAdapter<>(MainActivity.this, R.layout.support_simple_spinner_dropdown_item, seriesInfo.epInfo));
66+
infoListView.setAdapter(new ArrayAdapter<EpInfo>(MainActivity.this, R.layout.support_simple_spinner_dropdown_item, seriesInfo.epInfo){
67+
@Override
68+
public @NonNull View getView(int position, View convertView,@NonNull ViewGroup parent){
69+
View view=super.getView(position,convertView,parent);
70+
if (position==HistoryList.get(0).position){//永远是最前面的那个
71+
((TextView)view).setTextColor(Color.BLUE);
72+
}else ((TextView)view).setTextColor(Color.BLACK);
73+
return view;
74+
}
75+
});
6376
titleTextView = findViewById(R.id.titleTextView);
6477
LoadCookies();
6578
LoadHistory();
@@ -98,25 +111,20 @@ protected void onPostExecute(String successMsg) {
98111
//重启哔哩哔哩
99112
Intent intent=getPackageManager().getLaunchIntentForPackage(Settings.clientType.packageName);
100113
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
101-
startActivity(intent);
114+
// startActivity(intent);
102115
}
103116
else {//创建下载任务, 返回值是创建文件的路径
104117
for (int i = 0; i < seriesInfo.downloadSegmentInfo.size(); i++) {
105118
DownloadSegmentInfo downSegInfo = seriesInfo.downloadSegmentInfo.get(i);
106119
EpInfo epInfo = seriesInfo.epInfo.get(seriesInfo.position);
107-
DownloadTask(downSegInfo.url, successMsg + i + ".blv", seriesInfo.title + epInfo.index + epInfo.index_title + i);
120+
DownloadTask(downSegInfo.url, successMsg + i + ".blv", seriesInfo.title + epInfo.index + epInfo.index_title + i,MainActivity.this);
108121
}
109122
Toast.makeText(MainActivity.this, "成功: 正在下载文件\n需要关闭VPN", Toast.LENGTH_LONG).show();
110123
}
111124
int position = HistoryList.get(0).position;//历史记录中的位置
112125
if (position != seriesInfo.position) {
113-
if (position != -1) {
114-
EpInfo epInfo = seriesInfo.epInfo.get(position);
115-
epInfo.index_title = epInfo.index_title.substring(0, epInfo.index_title.length() - 1);
116-
}
117-
seriesInfo.epInfo.get(seriesInfo.position).index_title += "☆";
118-
((ArrayAdapter) infoListView.getAdapter()).notifyDataSetChanged();
119126
HistoryList.get(0).position = seriesInfo.position;
127+
((ArrayAdapter) infoListView.getAdapter()).notifyDataSetChanged();
120128
StoreHistory(getApplicationContext());
121129
}
122130
}
@@ -213,8 +221,7 @@ protected void onPostExecute(String HTMLBody) {
213221
HistoryInfo info = new HistoryInfo(seriesInfo.title, url + seriesInfo.season_id, -1);
214222
int position = HistoryList.indexOf(info);
215223
if (position > -1) {
216-
info.position = HistoryList.get(position).position;
217-
if (info.position > -1) seriesInfo.epInfo.get(info.position).index_title += "☆";
224+
info.position=HistoryList.get(position).position;
218225
HistoryList.remove(position);
219226
}
220227
HistoryList.add(0, info);
@@ -308,9 +315,9 @@ static void StoreHistory(Context context) {
308315
sharedPref.edit().putStringSet("HistoryList", HistoryStrSet).apply();
309316
}
310317

311-
void DownloadTask(String url, String filePath, String title) {
318+
static void DownloadTask(String url, String filePath, String title,Context context) {
312319
//关闭VPN
313-
DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
320+
DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
314321
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
315322
request.setAllowedOverMetered(false);
316323
request.setAllowedOverRoaming(false);

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.annotation.SuppressLint;
44
import android.content.Context;
55
import android.os.Bundle;
6+
import android.os.Environment;
67
import android.preference.PreferenceManager;
78
import android.view.View;
89
import android.widget.RelativeLayout;
@@ -29,7 +30,8 @@ public class Settings extends AppCompatActivity {
2930
static VideoQuality videoQuality;
3031
static ClientType clientType = ClientType.release;
3132
static boolean clientDownload;
32-
static String rootPath = "/storage/emulated/0/Android/data/";
33+
static String rootPath = Environment.getExternalStorageDirectory() + "/Android/data/";
34+
static String downloadAPKPath = Environment.getExternalStorageDirectory() + "/Download/";
3335

3436
@Override
3537
protected void onCreate(Bundle savedInstanceState) {

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

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,48 @@
2929
tools:context=".HistorySettingsActivity"
3030
tools:showIn="@layout/history_settings">
3131

32+
<TextView
33+
android:id="@+id/version"
34+
android:layout_width="0dp"
35+
android:layout_height="wrap_content"
36+
android:layout_marginStart="8dp"
37+
android:layout_marginTop="8dp"
38+
android:layout_marginEnd="8dp"
39+
android:gravity="center"
40+
app:layout_constraintEnd_toEndOf="parent"
41+
app:layout_constraintStart_toStartOf="parent"
42+
app:layout_constraintTop_toTopOf="parent" />
43+
44+
<Button
45+
android:id="@+id/updateButton"
46+
android:layout_width="wrap_content"
47+
android:layout_height="wrap_content"
48+
android:layout_margin="0dp"
49+
android:layout_marginStart="8dp"
50+
android:layout_marginEnd="8dp"
51+
android:allowUndo="false"
52+
android:clickable="true"
53+
android:focusable="true"
54+
android:minHeight="0dp"
55+
android:padding="8dp"
56+
android:text="更新"
57+
android:textSize="14sp"
58+
app:layout_constraintEnd_toEndOf="parent"
59+
app:layout_constraintStart_toStartOf="parent"
60+
app:layout_constraintTop_toBottomOf="@+id/version" />
61+
3262
<TextView
3363
android:id="@+id/aboutText"
3464
android:layout_width="0dp"
3565
android:layout_height="0dp"
3666
android:layout_marginStart="8dp"
3767
android:layout_marginTop="8dp"
3868
android:layout_marginEnd="8dp"
69+
android:focusable="true"
3970
app:layout_constraintBottom_toBottomOf="parent"
4071
app:layout_constraintEnd_toEndOf="parent"
4172
app:layout_constraintStart_toStartOf="parent"
42-
app:layout_constraintTop_toTopOf="parent"
43-
android:focusable="true" />
73+
app:layout_constraintTop_toBottomOf="@+id/updateButton" />
4474
</androidx.constraintlayout.widget.ConstraintLayout>
4575

4676
</androidx.coordinatorlayout.widget.CoordinatorLayout>

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,25 @@
1414
android:layout_width="0dp"
1515
android:layout_height="wrap_content"
1616
android:layout_marginStart="8dp"
17-
android:layout_marginEnd="8dp"
1817
android:ems="10"
1918
android:hint="@string/inputTextHint"
2019
android:inputType="text"
20+
android:minHeight="0dp"
2121
android:singleLine="true"
22-
app:layout_constraintEnd_toEndOf="parent"
23-
app:layout_constraintHorizontal_bias="1.0"
22+
app:layout_constraintEnd_toStartOf="@+id/searchButton"
2423
app:layout_constraintStart_toStartOf="parent"
2524
app:layout_constraintTop_toTopOf="parent" />
2625

2726
<Button
2827
android:id="@+id/searchButton"
2928
android:layout_width="wrap_content"
3029
android:layout_height="wrap_content"
31-
android:layout_marginStart="8dp"
32-
android:layout_marginEnd="4dp"
30+
android:layout_margin="0dp"
31+
android:minHeight="0dp"
3332
android:text="@string/searchButtonText"
33+
app:layout_constraintBottom_toBottomOf="@+id/inputText"
3434
app:layout_constraintEnd_toEndOf="parent"
35-
app:layout_constraintStart_toStartOf="parent"
36-
app:layout_constraintTop_toBottomOf="@+id/inputText" />
35+
app:layout_constraintTop_toTopOf="parent" />
3736

3837
<TextView
3938
android:id="@+id/titleTextView"
@@ -48,7 +47,7 @@
4847
android:textSize="18sp"
4948
app:layout_constraintEnd_toEndOf="parent"
5049
app:layout_constraintStart_toStartOf="parent"
51-
app:layout_constraintTop_toBottomOf="@+id/searchButton" />
50+
app:layout_constraintTop_toBottomOf="@+id/inputText" />
5251

5352
<ListView
5453
android:id="@+id/infoListView"
@@ -62,6 +61,6 @@
6261
app:layout_constraintBottom_toBottomOf="parent"
6362
app:layout_constraintEnd_toEndOf="parent"
6463
app:layout_constraintStart_toStartOf="parent"
65-
app:layout_constraintTop_toBottomOf="@id/titleTextView" />
64+
app:layout_constraintTop_toBottomOf="@+id/titleTextView" />
6665

6766
</androidx.constraintlayout.widget.ConstraintLayout>

0 commit comments

Comments
 (0)