44import android .content .DialogInterface ;
55import android .content .Intent ;
66import android .content .pm .PackageManager ;
7+ import android .net .Uri ;
78import android .os .Build ;
89import android .os .Bundle ;
10+ import android .os .Handler ;
11+ import android .os .Looper ;
912import android .support .v7 .app .AlertDialog ;
1013import android .support .v7 .app .AppCompatActivity ;
1114
2124
2225import io .github .javiewer .Configurations ;
2326import io .github .javiewer .JAViewer ;
27+ import io .github .javiewer .Properties ;
2428import io .github .javiewer .R ;
29+ import okhttp3 .Call ;
30+ import okhttp3 .Callback ;
31+ import okhttp3 .Request ;
32+ import okhttp3 .Response ;
2533
2634public class StartActivity extends AppCompatActivity {
2735
@@ -31,7 +39,68 @@ protected void onCreate(Bundle savedInstanceState) {
3139 super .onCreate (savedInstanceState );
3240 setContentView (R .layout .activity_start );
3341
34- checkPermissions ();
42+ checkPermissions (); //检查权限,创建配置
43+ }
44+
45+ public void readProperties () {
46+ Request request = new Request .Builder ()
47+ .url ("https://raw.githubusercontent.com/SplashCodes/JAViewer/master/properties.json" )
48+ .build ();
49+ JAViewer .HTTP_CLIENT .newCall (request ).enqueue (new Callback () {
50+ @ Override
51+ public void onFailure (Call call , IOException e ) {
52+ }
53+
54+ @ Override
55+ public void onResponse (Call call , Response response ) throws IOException {
56+ final Properties properties = JAViewer .parseJson (Properties .class , response .body ().string ());
57+ if (properties != null ) {
58+ new Handler (Looper .getMainLooper ()).post (new Runnable () {
59+ @ Override
60+ public void run () {
61+ handleProperties (properties );
62+ }
63+ });
64+ }
65+ }
66+ });
67+ }
68+
69+ public void handleProperties (Properties properties ) {
70+ JAViewer .DATA_SOURCES .clear ();
71+ JAViewer .DATA_SOURCES .addAll (properties .getDataSources ());
72+
73+
74+ int currentVersion ;
75+ try {
76+ currentVersion = this .getPackageManager ().getPackageInfo (this .getPackageName (), 0 ).versionCode ;
77+ } catch (PackageManager .NameNotFoundException e ) {
78+ throw new RuntimeException ("Hacked???" );
79+ }
80+
81+ if (properties .getLatestVersionCode () > 0 && currentVersion < properties .getLatestVersionCode ()) {
82+
83+ String message = "新版本:" + properties .getLatestVersion ();
84+ if (properties .getChangelog () != null ) {
85+ message += "\n \n 更新日志:\n \n " + properties .getChangelog () + "\n " ;
86+ }
87+
88+ AlertDialog dialog = new AlertDialog .Builder (this )
89+ .setTitle ("发现更新" )
90+ .setMessage (message )
91+ .setNegativeButton ("忽略更新" , null )
92+ .setPositiveButton ("更新" , new DialogInterface .OnClickListener () {
93+ @ Override
94+ public void onClick (DialogInterface dialog , int which ) {
95+ startActivity (new Intent (Intent .ACTION_VIEW , Uri .parse ("https://github.com/SplashCodes/JAViewer/releases" )));
96+ }
97+ })
98+ .create ();
99+ dialog .show ();
100+ }
101+
102+ startActivity (new Intent (StartActivity .this , MainActivity .class ));
103+ finish ();
35104 }
36105
37106 private void checkPermissions () {
@@ -84,8 +153,7 @@ public void onPermissionRationaleShouldBeShown(PermissionRequest permission, Per
84153
85154 JAViewer .CONFIGURATIONS = Configurations .load (config );
86155
87- startActivity (new Intent (StartActivity .this , MainActivity .class ));
88- finish ();
156+ readProperties ();
89157 }
90158
91159}
0 commit comments