11package com .brainor .bilihelper ;
22
3+ import android .app .DownloadManager ;
4+ import android .content .BroadcastReceiver ;
5+ import android .content .Context ;
36import android .content .Intent ;
7+ import android .content .IntentFilter ;
8+ import android .content .pm .PackageManager ;
49import android .os .AsyncTask ;
510import android .os .Bundle ;
611import android .text .Html ;
712import android .text .Spanned ;
813import android .text .method .LinkMovementMethod ;
14+ import android .util .Log ;
915import android .view .View ;
16+ import android .widget .Button ;
1017import android .widget .TextView ;
1118import android .widget .Toast ;
1219
2734public 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}
0 commit comments