|
|
@@ -1,25 +1,69 @@
|
|
|
package com.ht.gate;
|
|
|
|
|
|
import android.annotation.SuppressLint;
|
|
|
+import android.app.AlertDialog;
|
|
|
+import android.app.PendingIntent;
|
|
|
+import android.app.ProgressDialog;
|
|
|
import android.content.ComponentName;
|
|
|
+import android.content.Context;
|
|
|
import android.content.DialogInterface;
|
|
|
import android.content.Intent;
|
|
|
+import android.content.IntentSender;
|
|
|
import android.content.ServiceConnection;
|
|
|
import android.content.SharedPreferences;
|
|
|
+import android.content.pm.PackageInstaller;
|
|
|
+import android.net.Uri;
|
|
|
+import android.os.Build;
|
|
|
import android.os.Bundle;
|
|
|
+import android.os.Environment;
|
|
|
import android.os.IBinder;
|
|
|
import android.text.TextUtils;
|
|
|
import android.util.Log;
|
|
|
import android.view.View;
|
|
|
import android.widget.TextView;
|
|
|
+import android.widget.Toast;
|
|
|
|
|
|
+import androidx.annotation.NonNull;
|
|
|
+import androidx.annotation.Nullable;
|
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
|
+import androidx.core.content.FileProvider;
|
|
|
|
|
|
+import com.blankj.utilcode.constant.PermissionConstants;
|
|
|
+import com.blankj.utilcode.util.AppUtils;
|
|
|
+import com.blankj.utilcode.util.PathUtils;
|
|
|
+import com.blankj.utilcode.util.PermissionUtils;
|
|
|
+import com.blankj.utilcode.util.UtilsTransActivity;
|
|
|
+import com.ht.gate.domain.QueryUpdate;
|
|
|
import com.ht.gate.domain.Room;
|
|
|
+import com.ht.gate.domain.UpdateInfo;
|
|
|
+import com.liulishuo.filedownloader.BaseDownloadTask;
|
|
|
+import com.liulishuo.filedownloader.FileDownloadListener;
|
|
|
+import com.liulishuo.filedownloader.FileDownloader;
|
|
|
+import com.liulishuo.okdownload.DownloadListener;
|
|
|
+import com.liulishuo.okdownload.DownloadTask;
|
|
|
+import com.liulishuo.okdownload.StatusUtil;
|
|
|
+import com.liulishuo.okdownload.core.breakpoint.BreakpointInfo;
|
|
|
+import com.liulishuo.okdownload.core.cause.EndCause;
|
|
|
+import com.liulishuo.okdownload.core.cause.ResumeFailedCause;
|
|
|
+
|
|
|
+import org.apache.commons.io.FilenameUtils;
|
|
|
+import org.xutils.x;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.OutputStream;
|
|
|
+import java.nio.file.Paths;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
|
|
|
import butterknife.BindView;
|
|
|
import butterknife.ButterKnife;
|
|
|
import butterknife.OnClick;
|
|
|
+import retrofit2.Call;
|
|
|
+import retrofit2.Callback;
|
|
|
+import retrofit2.Response;
|
|
|
|
|
|
@SuppressLint("SetTextI18n")
|
|
|
public class SettingsActivity extends AppCompatActivity {
|
|
|
@@ -33,6 +77,8 @@ public class SettingsActivity extends AppCompatActivity {
|
|
|
TextView tvAddress;
|
|
|
@BindView(R.id.tv_site_name)
|
|
|
TextView tvSiteName;
|
|
|
+ @BindView(R.id.tv_version)
|
|
|
+ TextView tvVersion;
|
|
|
|
|
|
public static final String PREF_NAME_SETTINGS = "settings";
|
|
|
public static final String PREF_KEY_ADDRESS = "address";
|
|
|
@@ -83,6 +129,7 @@ public class SettingsActivity extends AppCompatActivity {
|
|
|
}
|
|
|
}
|
|
|
tvAddress.setText(sharedPreferences.getString(PREF_KEY_ADDRESS, ""));
|
|
|
+ tvVersion.setText(AppUtils.getAppVersionName() + " (" + AppUtils.getAppVersionCode() + ")");
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -135,4 +182,198 @@ public class SettingsActivity extends AppCompatActivity {
|
|
|
.show();
|
|
|
}
|
|
|
|
|
|
+ @OnClick(R.id.btn_update)
|
|
|
+ void checkUpdate() {
|
|
|
+// try {
|
|
|
+//
|
|
|
+// Room room = Room.fromBase64(sharedPreferences.getString(Constants.PREF_ROOM_INFO, null));
|
|
|
+// RetrofitManager.getInstance().getRetrofit().create(ApiService.class)
|
|
|
+// .checkUpdate(new QueryUpdate(room.getSiteCode(), "02", AppUtils.getAppVersionCode() + ""))
|
|
|
+// .enqueue(new Callback<UpdateInfo>() {
|
|
|
+// @Override
|
|
|
+// public void onResponse(Call<UpdateInfo> call, Response<UpdateInfo> response) {
|
|
|
+// if (response.code() == 200) {
|
|
|
+// if (response.body() == null) {
|
|
|
+// return;
|
|
|
+// }
|
|
|
+// if (response.body().getResultData().isNeedUpdate()) {
|
|
|
+// PermissionUtils.permission(PermissionConstants.STORAGE).rationale(new PermissionUtils.OnRationaleListener() {
|
|
|
+// @Override
|
|
|
+// public void rationale(UtilsTransActivity activity, ShouldRequest shouldRequest) {
|
|
|
+// }
|
|
|
+// }).callback(new PermissionUtils.SimpleCallback() {
|
|
|
+// @Override
|
|
|
+// public void onGranted() {
|
|
|
+// startDownload(RetrofitManager.BASE_URL.replaceAll("/$", "") + response.body().getResultData().getSoftwareUrl());
|
|
|
+// }
|
|
|
+//
|
|
|
+// @Override
|
|
|
+// public void onDenied() {
|
|
|
+//
|
|
|
+// }
|
|
|
+// }).request();
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// @Override
|
|
|
+// public void onFailure(Call<UpdateInfo> call, Throwable t) {
|
|
|
+//
|
|
|
+// }
|
|
|
+// });
|
|
|
+// } catch (Exception e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
+// startDownload("http://dl-cdn.coolapkmarket.com/down/apk_file/2020/0518/Coolapk-v10.2-2005181-coolapk-app-release.apk");
|
|
|
+ installApk(new File("file:///sdcard/Android/data/com.ht.gate/files/Download/1589872246972.apk"));
|
|
|
+ }
|
|
|
+
|
|
|
+ private void startDownload(String url) {
|
|
|
+ FileDownloader.setup(SettingsActivity.this);
|
|
|
+ ProgressDialog progressDialog = new ProgressDialog(this, AlertDialog.THEME_DEVICE_DEFAULT_DARK);
|
|
|
+ progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
|
|
|
+ progressDialog.setCanceledOnTouchOutside(false);
|
|
|
+ progressDialog.setCancelable(false);
|
|
|
+ progressDialog.setMessage("下载中");
|
|
|
+ progressDialog.setProgress(0);
|
|
|
+ String ext = FilenameUtils.getExtension(url);
|
|
|
+ String fileName = System.currentTimeMillis() + "." + ext;
|
|
|
+ progressDialog.show();
|
|
|
+ new DownloadTask.Builder(url, new File(PathUtils.getExternalAppDownloadPath()))
|
|
|
+ .setFilename(fileName)
|
|
|
+ // the minimal interval millisecond for callback progress
|
|
|
+ .setMinIntervalMillisCallbackProcess(16)
|
|
|
+ // ignore the same task has already completed in the past.
|
|
|
+ .setPassIfAlreadyCompleted(false)
|
|
|
+ .build()
|
|
|
+ .enqueue(new DownloadListener() {
|
|
|
+ @Override
|
|
|
+ public void taskStart(@NonNull DownloadTask task) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void connectTrialStart(@NonNull DownloadTask task, @NonNull Map<String, List<String>> requestHeaderFields) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void connectTrialEnd(@NonNull DownloadTask task, int responseCode, @NonNull Map<String, List<String>> responseHeaderFields) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void downloadFromBeginning(@NonNull DownloadTask task, @NonNull BreakpointInfo info, @NonNull ResumeFailedCause cause) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void downloadFromBreakpoint(@NonNull DownloadTask task, @NonNull BreakpointInfo info) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void connectStart(@NonNull DownloadTask task, int blockIndex, @NonNull Map<String, List<String>> requestHeaderFields) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void connectEnd(@NonNull DownloadTask task, int blockIndex, int responseCode, @NonNull Map<String, List<String>> responseHeaderFields) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void fetchStart(@NonNull DownloadTask task, int blockIndex, long contentLength) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void fetchProgress(@NonNull DownloadTask task, int blockIndex, long increaseBytes) {
|
|
|
+ long total = StatusUtil.getCurrentInfo(task).getTotalLength();
|
|
|
+ long current = StatusUtil.getCurrentInfo(task).getTotalOffset();
|
|
|
+ if (total != 0)
|
|
|
+ progressDialog.setProgress((int) (current * 100 / total));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void fetchEnd(@NonNull DownloadTask task, int blockIndex, long contentLength) {
|
|
|
+ progressDialog.dismiss();
|
|
|
+ Log.d("xxxx", "fetchEnd");
|
|
|
+
|
|
|
+ installApk(new File(PathUtils.getExternalAppDownloadPath() + fileName));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void taskEnd(@NonNull DownloadTask task, @NonNull EndCause cause, @Nullable Exception realCause) {
|
|
|
+ progressDialog.dismiss();
|
|
|
+ Log.d("xxxx", "taskEnd");
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ private void installApk(File apkFile) {
|
|
|
+ Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
|
|
|
+ intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
|
|
+ intent.setDataAndType(FileProvider.getUriForFile(SettingsActivity.this,
|
|
|
+ "com.ht.gate.fileProvider", apkFile), "application/vnd.android.package-archive");
|
|
|
+ startActivity(intent);
|
|
|
+// AppUtils.installApp(apkFile);
|
|
|
+// try {
|
|
|
+// installPackage(apkFile.getPath());
|
|
|
+// } catch (IOException e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void installPackage(String filename) throws IOException {
|
|
|
+ PackageInstaller packageInstaller = getApplicationContext().getPackageManager().getPackageInstaller();
|
|
|
+ Log.i(TAG, "JIntentActionInstallApk - got packageInstaller");
|
|
|
+ PackageInstaller.SessionParams params = new PackageInstaller.SessionParams(
|
|
|
+ PackageInstaller.SessionParams.MODE_FULL_INSTALL);
|
|
|
+ Log.i(TAG, "JIntentActionInstallApk - set SessionParams");
|
|
|
+ int sessionId = packageInstaller.createSession(params);
|
|
|
+ PackageInstaller.Session session = packageInstaller.openSession(sessionId);
|
|
|
+ Log.i(TAG, "JIntentActionInstallApk - session opened");
|
|
|
+
|
|
|
+ // Create an install status receiver.
|
|
|
+ Context context = getApplicationContext();
|
|
|
+ addApkToInstallSession(context, filename, session);
|
|
|
+ Log.i(TAG, "JIntentActionInstallApk - apk added to session");
|
|
|
+
|
|
|
+ Intent intent = new Intent(context, MainActivity.class);
|
|
|
+ intent.setAction("PACKAGE_INSTALLED_ACTION");
|
|
|
+ PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
|
|
|
+ IntentSender statusReceiver = pendingIntent.getIntentSender();
|
|
|
+ // Commit the session (this will start the installation workflow).
|
|
|
+ session.commit(statusReceiver);
|
|
|
+ Log.i(TAG, "JIntentActionInstallApk - commited");
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void addApkToInstallSession(Context context, String filename, PackageInstaller.Session session) {
|
|
|
+ Log.i(TAG, "addApkToInstallSession " + filename);
|
|
|
+ // It's recommended to pass the file size to openWrite(). Otherwise installation may fail
|
|
|
+ // if the disk is almost full.
|
|
|
+ try {
|
|
|
+ OutputStream packageInSession = session.openWrite("package", 0, -1);
|
|
|
+ InputStream input;
|
|
|
+ Uri uri = Uri.parse(filename);
|
|
|
+ input = context.getContentResolver().openInputStream(uri);
|
|
|
+
|
|
|
+ if (input != null) {
|
|
|
+ Log.i(TAG, "input.available: " + input.available());
|
|
|
+ byte[] buffer = new byte[16384];
|
|
|
+ int n;
|
|
|
+ while ((n = input.read(buffer)) >= 0) {
|
|
|
+ packageInSession.write(buffer, 0, n);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ Log.i(TAG, "addApkToInstallSession failed");
|
|
|
+ throw new IOException("addApkToInstallSession");
|
|
|
+ }
|
|
|
+ packageInSession.close(); //need to close this stream
|
|
|
+ input.close(); //need to close this stream
|
|
|
+ } catch (Exception e) {
|
|
|
+ Log.i(TAG, "addApkToInstallSession failed2 " + e.toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|