mirror of https://github.com/FongMi/TV.git
parent
7d9ed7a094
commit
3dde328dce
@ -1,39 +1,87 @@ |
||||
package com.fongmi.android.tv.utils; |
||||
|
||||
import android.app.Activity; |
||||
import android.os.Handler; |
||||
import android.os.Looper; |
||||
import android.view.LayoutInflater; |
||||
import android.view.View; |
||||
|
||||
import androidx.appcompat.app.AlertDialog; |
||||
|
||||
import com.fongmi.android.tv.BuildConfig; |
||||
import com.fongmi.android.tv.R; |
||||
import com.fongmi.android.tv.databinding.DialogUpdateBinding; |
||||
import com.fongmi.android.tv.net.OKHttp; |
||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder; |
||||
|
||||
import org.json.JSONObject; |
||||
|
||||
import java.io.File; |
||||
import java.util.concurrent.ExecutorService; |
||||
import java.util.concurrent.Executors; |
||||
|
||||
public class Updater { |
||||
public class Updater implements View.OnClickListener { |
||||
|
||||
private static final String URL = "https://github.com/FongMi/TV/raw/main/release/leanback.json"; |
||||
private static final String PROXY = "https://ghproxy.com/"; |
||||
|
||||
public static void check(Activity activity) { |
||||
new Thread(() -> new Updater().connect(URL, 0)).start(); |
||||
private final ExecutorService executor; |
||||
private final Activity activity; |
||||
private final Handler handler; |
||||
private AlertDialog dialog; |
||||
|
||||
public static Updater check(Activity activity) { |
||||
return new Updater(activity); |
||||
} |
||||
|
||||
public Updater(Activity activity) { |
||||
this.executor = Executors.newSingleThreadExecutor(); |
||||
this.handler = new Handler(Looper.getMainLooper()); |
||||
this.activity = activity; |
||||
} |
||||
|
||||
private File getApk() { |
||||
return FileUtil.getCacheFile("update.apk"); |
||||
} |
||||
|
||||
public void run() { |
||||
executor.submit(this::doInBackground); |
||||
} |
||||
|
||||
private void doInBackground() { |
||||
connect(URL, 0); |
||||
} |
||||
|
||||
private void connect(String target, int retry) { |
||||
try { |
||||
JSONObject object = new JSONObject(OKHttp.newCall(target).execute().body().string()); |
||||
int version = object.optInt("version"); |
||||
String url = object.optString("url"); |
||||
String name = object.optString("name"); |
||||
String desc = object.optString("desc"); |
||||
String url = object.optString("url"); |
||||
int code = object.optInt("code"); |
||||
if (retry > 0) url = PROXY + url; |
||||
if (version <= BuildConfig.VERSION_CODE) FileUtil.clearDir(getApk()); |
||||
else FileUtil.openFile(FileUtil.write(getApk(), OKHttp.newCall(url).execute().body().bytes())); |
||||
if (code <= BuildConfig.VERSION_CODE) FileUtil.clearDir(getApk()); |
||||
else FileUtil.write(getApk(), OKHttp.newCall(url).execute().body().bytes()); |
||||
if (getApk().exists()) handler.post(() -> showDialog(name, desc)); |
||||
} catch (Exception e) { |
||||
if (retry == 0) connect(PROXY + target, 1); |
||||
e.printStackTrace(); |
||||
} |
||||
} |
||||
|
||||
private void showDialog(String version, String desc) { |
||||
DialogUpdateBinding binding = DialogUpdateBinding.inflate(LayoutInflater.from(activity)); |
||||
dialog = new MaterialAlertDialogBuilder(activity).setView(binding.getRoot()).create(); |
||||
binding.version.setText(ResUtil.getString(R.string.update_version, version)); |
||||
binding.confirm.setOnClickListener(this); |
||||
binding.cancel.setOnClickListener(this); |
||||
binding.desc.setText(desc); |
||||
dialog.show(); |
||||
} |
||||
|
||||
@Override |
||||
public void onClick(View view) { |
||||
if (view.getId() == R.id.confirm) FileUtil.openFile(getApk()); |
||||
dialog.dismiss(); |
||||
} |
||||
} |
||||
|
||||
@ -0,0 +1,67 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||
xmlns:tools="http://schemas.android.com/tools" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:orientation="vertical" |
||||
android:padding="24dp"> |
||||
|
||||
<TextView |
||||
android:id="@+id/version" |
||||
android:layout_width="wrap_content" |
||||
android:layout_height="wrap_content" |
||||
android:letterSpacing="0.02" |
||||
android:textColor="@color/grey_900" |
||||
android:textSize="18sp" |
||||
tools:text="@string/update_version" /> |
||||
|
||||
<View |
||||
android:layout_width="match_parent" |
||||
android:layout_height="1dp" |
||||
android:layout_marginTop="16dp" |
||||
android:layout_marginBottom="16dp" |
||||
android:background="@color/grey_300" /> |
||||
|
||||
<TextView |
||||
android:id="@+id/desc" |
||||
android:layout_width="wrap_content" |
||||
android:layout_height="wrap_content" |
||||
android:layout_marginBottom="16dp" |
||||
android:letterSpacing="0.02" |
||||
android:lineSpacingExtra="8dp" |
||||
android:textColor="@color/grey_900" |
||||
android:textSize="16sp" |
||||
tools:text="1. 新增 ffmpeg 音頻軟解\n2. 詳情頁新增分詞快搜\n3. 修復搜尋閃退問題\n4. 設定支援渲染切換" /> |
||||
|
||||
<LinearLayout |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:orientation="horizontal"> |
||||
|
||||
<TextView |
||||
android:id="@+id/confirm" |
||||
android:layout_width="0dp" |
||||
android:layout_height="wrap_content" |
||||
android:layout_marginEnd="16dp" |
||||
android:layout_weight="1" |
||||
android:background="@drawable/selector_text" |
||||
android:focusable="true" |
||||
android:focusableInTouchMode="true" |
||||
android:gravity="center" |
||||
android:text="@string/update_confirm" |
||||
android:textColor="@color/white" /> |
||||
|
||||
<TextView |
||||
android:id="@+id/cancel" |
||||
android:layout_width="0dp" |
||||
android:layout_height="wrap_content" |
||||
android:layout_weight="1" |
||||
android:background="@drawable/selector_text" |
||||
android:focusable="true" |
||||
android:focusableInTouchMode="true" |
||||
android:gravity="center" |
||||
android:text="@string/dialog_negative" |
||||
android:textColor="@color/white" /> |
||||
|
||||
</LinearLayout> |
||||
</LinearLayout> |
||||
Loading…
Reference in new issue