added ota
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
package ru.vtosters.lite.ui.fragments;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.ProgressDialog;
|
||||
import android.content.Context;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.vk.core.dialogs.VKProgressDialog;
|
||||
|
||||
import ru.vtosters.lite.utils.OTAHelper;
|
||||
|
||||
public class OTADialog implements OTAHelper.OTAListener {
|
||||
|
||||
private Activity mActivity;
|
||||
private VKProgressDialog mProgressDialog;
|
||||
|
||||
private OTAHelper mHelper;
|
||||
|
||||
public static OTADialog checkUpdates(Activity activity) {
|
||||
return new OTADialog(activity);
|
||||
}
|
||||
|
||||
public OTADialog(Activity activity) {
|
||||
mActivity = activity;
|
||||
|
||||
mProgressDialog = new VKProgressDialog(activity);
|
||||
mProgressDialog.setTitle("Проверка обновлений...");
|
||||
mProgressDialog.setCancelable(false);
|
||||
mProgressDialog.show();
|
||||
|
||||
mHelper = new OTAHelper(this);
|
||||
mHelper.loadData();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUpdateApplied() {
|
||||
mActivity.runOnUiThread(() -> {
|
||||
Toast.makeText(mActivity, "Обновления найдены", Toast.LENGTH_SHORT).show();
|
||||
mProgressDialog.dismiss();
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUpdateCanceled() {
|
||||
mActivity.runOnUiThread(() -> {
|
||||
Toast.makeText(mActivity, "Обновлений не найдено", Toast.LENGTH_SHORT).show();
|
||||
mProgressDialog.dismiss();
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -337,7 +337,8 @@ public class VTSettings extends MaterialPreferenceToolbarFragment {
|
||||
});
|
||||
|
||||
PreferencesUtil.addPreference(this, "", getString("download_latest_vtl"), "", "ic_download_24", preference -> {
|
||||
getContext().startActivity(new Intent("android.intent.action.VIEW").setData(Uri.parse("https://github.com/vtosters/lite/releases/latest")));
|
||||
// getContext().startActivity(new Intent("android.intent.action.VIEW").setData(Uri.parse("https://github.com/vtosters/lite/releases/latest")));
|
||||
OTADialog.checkUpdates(p());
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -0,0 +1,126 @@
|
||||
package ru.vtosters.lite.utils;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import okhttp3.Call;
|
||||
import okhttp3.Callback;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
|
||||
public class OTAHelper {
|
||||
|
||||
private static final String LATEST_RELEASE_URL = "https://api.github.com/repositories/473685743/releases/latest";
|
||||
private static final String LATEST_RELEASE_COMMIT_URL = "https://api.github.com/repositories/473685743/git/ref/tags/%s";
|
||||
|
||||
private OTAListener mListener;
|
||||
|
||||
private final OkHttpClient mClient = new OkHttpClient();
|
||||
|
||||
private JSONObject mReleaseJson;
|
||||
private JSONObject mCommitJson;
|
||||
|
||||
public OTAHelper(OTAListener listener) {
|
||||
this.mListener = listener;
|
||||
}
|
||||
|
||||
public void loadData() {
|
||||
Request release = new Request.Builder()
|
||||
.url(LATEST_RELEASE_URL)
|
||||
.build();
|
||||
mClient.newCall(release).a(new Callback() {
|
||||
@Override
|
||||
public void a(Call call, IOException e) {
|
||||
Log.d("OTAHelper", "Error while getting latest release info: " + e);
|
||||
mListener.onUpdateCanceled();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void a(Call call, Response response) throws IOException {
|
||||
try {
|
||||
setData(response);
|
||||
} catch (JSONException | NullPointerException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void setData(Response response) throws IOException, JSONException {
|
||||
mReleaseJson = new JSONObject(response.body().string());
|
||||
|
||||
String tag = mReleaseJson.getString("tag_name");
|
||||
Request commit = new Request.Builder()
|
||||
.url(String.format(LATEST_RELEASE_COMMIT_URL, tag))
|
||||
.build();
|
||||
mClient.newCall(commit).a(new Callback() {
|
||||
@Override
|
||||
public void a(Call call, IOException e) {
|
||||
Log.d("OTAHelper", "Error while getting latest commit info: " + e);
|
||||
mListener.onUpdateCanceled();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void a(Call call, Response response) throws IOException {
|
||||
try {
|
||||
mCommitJson = new JSONObject(response.body().string());
|
||||
if (isNewVersion())
|
||||
mListener.onUpdateApplied();
|
||||
else mListener.onUpdateCanceled();
|
||||
} catch (NullPointerException | JSONException e) {
|
||||
e.printStackTrace();
|
||||
mListener.onUpdateCanceled();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public boolean isNewVersion() {
|
||||
try {
|
||||
String commitSHA = mCommitJson.getJSONObject("object").getString("sha");
|
||||
return !commitSHA.startsWith(About.getBuildNumber());
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public String getNewVersionName() {
|
||||
try {
|
||||
return mReleaseJson.getString("name");
|
||||
} catch (JSONException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public String getUpdateDescription() {
|
||||
try {
|
||||
return mReleaseJson.getString("body");
|
||||
} catch (JSONException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public String getDownloadUrl() {
|
||||
try {
|
||||
return mReleaseJson.getJSONArray("assets")
|
||||
.getJSONObject(0)
|
||||
.getString("browser_download_url");
|
||||
} catch (JSONException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public interface OTAListener {
|
||||
|
||||
void onUpdateApplied();
|
||||
|
||||
void onUpdateCanceled();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -66,7 +66,7 @@
|
||||
<uses-permission android:name="me.everything.badger.permission.BADGE_COUNT_READ"/>
|
||||
<uses-permission android:name="me.everything.badger.permission.BADGE_COUNT_WRITE"/>
|
||||
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
|
||||
<application android:allowBackup="false" android:debuggable="false" android:hardwareAccelerated="true" android:icon="@mipmap/vt_launcher" android:label="@string/app_name" android:largeHeap="true" android:requestLegacyExternalStorage="true" android:preserveLegacyExternalStorage="true" android:name="com.vtosters.lite.VKApplication" android:networkSecurityConfig="@xml/network_security_config" android:resizeableActivity="true" android:roundIcon="@mipmap/vt_launcher_round" android:supportsRtl="true" android:theme="@style/BaseStyle">
|
||||
<application android:allowBackup="false" android:debuggable="true" android:hardwareAccelerated="true" android:icon="@mipmap/vt_launcher" android:label="@string/app_name" android:largeHeap="true" android:requestLegacyExternalStorage="true" android:preserveLegacyExternalStorage="true" android:name="com.vtosters.lite.VKApplication" android:networkSecurityConfig="@xml/network_security_config" android:resizeableActivity="true" android:roundIcon="@mipmap/vt_launcher_round" android:supportsRtl="true" android:theme="@style/BaseStyle">
|
||||
<meta-data android:name="com.google.android.geo.API_KEY" android:value="AIzaSyBlTHhNGPj7ktPWrUo_d4YJs_Sn_zsISZg"/>
|
||||
<meta-data android:name="com.google.android.geo.API_SECRET" android:value="yu6ZAA0OWO3kmr4es7fGtSmiyfk="/>
|
||||
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/fb_app_id"/>
|
||||
|
||||
Reference in New Issue
Block a user