Skip to content

Commit

Permalink
自动检查更新
Browse files Browse the repository at this point in the history
  • Loading branch information
cbwang2016 committed Dec 4, 2018
1 parent 7c11c52 commit 2e206e5
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 8 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId "edu_cn.pku.course.pkucourse"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
versionCode 1000
versionName "beta v0.1.5"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
}
Expand Down
22 changes: 21 additions & 1 deletion app/src/main/java/edu_cn/pku/course/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import edu_cn.pku.course.activities.SplashActivity;

public class Utils {
private static final String versionString = "beta v0.1.4";
public static final String versionString = "beta v0.1.5";
public static final String errorPrefix = "Error: ";
public static final String errorPasswordIncorrect = "Password Incorrect";
private static final String errorSubstrings = "error when extracting substrings";
Expand Down Expand Up @@ -312,4 +312,24 @@ public static String getPortalSession() {
}
}
}

static public String getGithubApiResponse() {
HttpURLConnection conn = null;
try {
String request = "https://api.github.com/repos/cbwang2016/PKUCourses/releases/latest";
URL url = new URL(request);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setUseCaches(false);
InputStream in = conn.getInputStream();

return convertStreamToString(in);
} catch (Exception e) {
return errorPrefix + e.getMessage();
} finally {
if (conn != null) {
conn.disconnect();
}
}
}
}
53 changes: 53 additions & 0 deletions app/src/main/java/edu_cn/pku/course/activities/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
Expand All @@ -21,9 +23,13 @@
import android.view.View;
import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import edu_cn.pku.course.Utils;
import edu_cn.pku.course.adapter.FragmentAdapter;
import edu_cn.pku.course.fragments.AnnouncementListFragment;
import edu_cn.pku.course.fragments.CourseListFragment;
Expand Down Expand Up @@ -111,6 +117,10 @@ protected void onCreate(Bundle savedInstanceState) {
setTitle(navigationView.getMenu().getItem(launch_page_index).getTitle());
navigationView.getMenu().getItem(launch_page_index).setChecked(true);
view_pager_main.setCurrentItem(launch_page_index);

if (sp.getBoolean("auto_check_update", true)) {
new CheckUpdateTask().execute((Void) null);
}
}

private boolean checkLongPressHint() {
Expand Down Expand Up @@ -212,4 +222,47 @@ private void signOut() {
startActivity(intent);
finish();
}

@SuppressLint("StaticFieldLeak")
private class CheckUpdateTask extends AsyncTask<Void, Void, String> {
CheckUpdateTask() {
}

@Override
protected String doInBackground(Void... params) {
return Utils.getGithubApiResponse();
}


@Override
protected void onPostExecute(final String str) {
if (!str.startsWith(Utils.errorPrefix)) {
try {
JSONObject jo = new JSONObject(str);
String latestVersionName = jo.getString("name");
if (!latestVersionName.equals(Utils.versionString)) {
String latestReleaseBody = jo.getString("body");
final String latestReleaseDownloadUrl = ((JSONObject) jo.getJSONArray("assets").get(0)).getString("browser_download_url");

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this, R.style.AlertDialogTheme);
builder.setTitle("检测到新版本: " + latestVersionName);
builder.setMessage("更新内容:\n" + latestReleaseBody);
builder.setPositiveButton("下载", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(latestReleaseDownloadUrl));
startActivity(browserIntent);
}
});
builder.setNegativeButton("取消", null).show();
}
} catch (JSONException e) {
// e.printStackTrace();
}
}
}

@Override
protected void onCancelled() {
}
}
}
10 changes: 5 additions & 5 deletions app/src/main/res/xml/preferences_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

<PreferenceCategory android:title="@string/settings_category_basic">
<!--<SwitchPreference-->
<!--android:defaultValue="true"-->
<!--android:key="example_switch"-->
<!--android:summary="@string/settings_switch_preference_summary"-->
<!--android:title="@string/settings_switch_preference_title" />-->
<SwitchPreference
android:defaultValue="true"
android:key="auto_check_update"
android:summary="每次程序启动时会检查更新"
android:title="自动检查更新" />

<!-- NOTE: Hide buttons to simplify the UI. Users can touch outside the dialog to dismiss it. -->
<!-- NOTE: ListPreference's summary should be set to its value by the activity code. -->
Expand Down

0 comments on commit 2e206e5

Please sign in to comment.