Commit 1301b7024e64ac084d75fe773926a4872f8d5e68

Authored by Penley
1 parent 26fc7401

system update with ft

... ... @@ -148,6 +148,9 @@ public class AdsPreVideoPlayerActivity extends BaseActivity {
148 148 }
149 149
150 150 public String getAdsVideoUrl() {
151   - return Utils.getSambaMsg(this).getLocalPath() + "ads.mp4";
  151 +// return Utils.getSambaMsg(this).getLocalPath() + "ads.mp4";
  152 +// return "rtsp://192.168.31.197/8554/1";
  153 + return "http://192.168.31.197:8080/1";
  154 +// return "http://192.168.200.193/qnykflv.flv";
152 155 }
153 156 }
\ No newline at end of file
... ...
  1 +/*
  2 + * Copyright (c) 2017. wugian
  3 + * Licensed under the Apache License, Version 2.0 (the "License");
  4 + * you may not use this file except in compliance with the License.
  5 + * You may obtain a copy of the License at
  6 + *
  7 + * http://www.apache.org/licenses/LICENSE-2.0
  8 + *
  9 + * Unless required by applicable law or agreed to in writing, software
  10 + * distributed under the License is distributed on an "AS IS" BASIS,
  11 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12 + * See the License for the specific language governing permissions and
  13 + * limitations under the License.
  14 + *
  15 + */
  16 +
  17 +package com.xgimi.gimicinema.appupdate;
  18 +
  19 +import android.app.AlertDialog;
  20 +import android.app.Dialog;
  21 +import android.content.Context;
  22 +import android.content.DialogInterface;
  23 +import android.content.pm.PackageManager;
  24 +import android.os.Handler;
  25 +import android.os.Message;
  26 +import android.text.TextUtils;
  27 +import android.util.Log;
  28 +
  29 +import com.gimi.common.cinema.utils.Config;
  30 +import com.gimi.common.cinema.utils.OkHttpClientManager;
  31 +import com.gimi.common.cinema.utils.SystemUtils;
  32 +import com.gimi.common.cinema.utils.Utils;
  33 +import com.gimi.common.cinema.utils.XgimiDevice;
  34 +import com.google.gson.Gson;
  35 +import com.google.gson.JsonSyntaxException;
  36 +import com.google.gson.reflect.TypeToken;
  37 +import com.xgimi.gimicinema.BuildConfig;
  38 +
  39 +import java.lang.ref.WeakReference;
  40 +
  41 +/**
  42 + * Created by wugian on 2017/4/24
  43 + */
  44 +public class AppUpgradeManager {
  45 + private static final String TAG = "AppUpgradeManager";
  46 + private static final int SHOW_DIALOG_UPDATE = 0x91;
  47 + private static final int SHOW_DIALOG_NEWEST = 0x92;
  48 + private String API_ROOT = "http://api.qnbar.com";
  49 + private static final String API_ROOT_TEST = "http://api.t.qnbar.com";
  50 + private static final String API_GET_APP_VERSION = "/Api/Agent/CheckAppVersion1";
  51 + // private static final String API_GET_APP_VERSION = "/Agent/CheckAppVersion1";
  52 + private String updateApkUrl;// = API_ROOT + "/Api/Agent/CheckAppVersion1";// + XgimiDevice.getDeviceType() + "&imei=" + SystemUtils.getPID();
  53 + private UpdateMsg mUpdateMsg;
  54 + private Context context;
  55 + private String packageName = "";
  56 + private boolean showUpdate;
  57 + private Handler mHandler;
  58 +
  59 + public AppUpgradeManager(Context context, String packageName, boolean showUpdates) {
  60 + boolean test = Utils.getBool(context, "test");
  61 + if (test) {
  62 + API_ROOT = API_ROOT_TEST;
  63 + }
  64 + this.context = context;
  65 + this.packageName = packageName;
  66 + this.showUpdate = showUpdates;
  67 + this.mHandler = new AppUpdateHandler(this);
  68 + }
  69 +
  70 + private static class AppUpdateHandler extends Handler {
  71 + private final WeakReference<AppUpgradeManager> activity;
  72 +
  73 + AppUpdateHandler(AppUpgradeManager activity) {
  74 + this.activity = new WeakReference<>(activity);
  75 + }
  76 +
  77 + @Override
  78 + public void handleMessage(Message msg) {
  79 + super.handleMessage(msg);
  80 + AppUpgradeManager a = activity.get();
  81 + if (a != null) {
  82 + switch (msg.what) {
  83 + case SHOW_DIALOG_UPDATE:
  84 + a.doNewVersionUpdate();
  85 + break;
  86 + case SHOW_DIALOG_NEWEST:
  87 + a.notNewVersionShow();
  88 + break;
  89 + }
  90 + }
  91 + }
  92 + }
  93 +
  94 +
  95 + public boolean updateAppVersion() {
  96 + String verJson = null;
  97 + String deviceType = "8888";
  98 + String pid = SystemUtils.getPid(context, BuildConfig.MACHINE_TYPE);
  99 + if (!BuildConfig.MACHINE_TYPE.equals("himedia")) {
  100 + deviceType = XgimiDevice.getDeviceType();
  101 + }
  102 + String pkgName = packageName;
  103 + if ("com.xgimi.gimicinema".equals(packageName)) {
  104 + pkgName = pkgName + ".ft";
  105 + }
  106 + updateApkUrl = API_ROOT + API_GET_APP_VERSION + "?machineType=" + deviceType + "&imei=" + pid + "&packageName=" + pkgName;
  107 + Log.d("install", "request:" + updateApkUrl);
  108 + try {
  109 + verJson = OkHttpClientManager.getAsString(updateApkUrl);
  110 + Log.d("install", "result:" + verJson);
  111 + } catch (Exception e) {
  112 + e.printStackTrace();
  113 + }
  114 +
  115 + if (TextUtils.isEmpty(verJson)) {
  116 + Log.d("install", "verJson charge empty");
  117 + return false;
  118 + }
  119 + try {
  120 + UpdateMsg updateMsg = new Gson().fromJson(verJson, new TypeToken<UpdateMsg>() {
  121 + }.getType());
  122 + if (updateMsg != null && updateMsg.getCode() == 0
  123 + && updateMsg.getData() != null) {
  124 + Log.d("install", "charge version");
  125 + if (updateMsg.getData().getVerCode() > getVerCode(context)) {
  126 + this.mUpdateMsg = updateMsg;
  127 + mHandler.sendEmptyMessage(SHOW_DIALOG_UPDATE);
  128 +// doNewVersionUpdate();
  129 + } else {
  130 + Log.d("install", "charge empty");
  131 + if (showUpdate) {
  132 +// notNewVersionShow();
  133 + mHandler.sendEmptyMessage(SHOW_DIALOG_NEWEST);
  134 + }
  135 + }
  136 + } else {
  137 + Log.d("install", "charge 123");
  138 + if (showUpdate) {
  139 +// notNewVersionShow();
  140 + mHandler.sendEmptyMessage(SHOW_DIALOG_NEWEST);
  141 + }
  142 + }
  143 + } catch (JsonSyntaxException e) {
  144 + e.printStackTrace();
  145 + }
  146 + return true;
  147 + }
  148 +
  149 +
  150 + private void doNewVersionUpdate() {
  151 + Log.d("install", "doNewVersionUpdate");
  152 + if (!showUpdate && mUpdateMsg.getData().isForceType()) {
  153 + String url = mUpdateMsg.getData().getAppUrl();
  154 + Log.d("install", "doNewVersionUpdate:" + url);
  155 + if (!TextUtils.isEmpty(url)) {
  156 + UpgradeDownloadManager updateManager =
  157 + new UpgradeDownloadManager(context, url, mUpdateMsg.getData().isForceType());
  158 + updateManager.downloadApk(showUpdate | !mUpdateMsg.getData().isForceType());
  159 + }
  160 + return;
  161 + }
  162 + String verName = Config.getVerName(context);
  163 + StringBuilder message = new StringBuilder();
  164 + message.append("当前版本");
  165 + message.append(verName);
  166 + message.append(", 发现新版本");
  167 + message.append(mUpdateMsg.getData().getVerName());
  168 + message.append(", 是否更新?");
  169 + message.append("\n");
  170 + String description = mUpdateMsg.getData().getDescription();
  171 + if (!TextUtils.isEmpty(description)) {
  172 + if (description.contains("#")) {
  173 + String[] split = description.split("#");
  174 + for (String s : split) {
  175 + message.append("\n").append(s);
  176 + }
  177 + } else {
  178 + message.append("\n").append(mUpdateMsg);
  179 + }
  180 + }
  181 + Dialog dialog = new AlertDialog.Builder(context)
  182 + .setTitle("软件更新")
  183 + .setMessage(message.toString())
  184 + .setCancelable(false)
  185 + // 设置内容
  186 + .setPositiveButton("更新",// 设置确定按钮
  187 + new DialogInterface.OnClickListener() {
  188 + @Override
  189 + public void onClick(DialogInterface dialog1, int which) {
  190 + String url = mUpdateMsg.getData().getAppUrl();
  191 + if (!TextUtils.isEmpty(url)) {
  192 + UpgradeDownloadManager updateManager =
  193 + new UpgradeDownloadManager(context, url, showUpdate & mUpdateMsg.getData().isForceType());
  194 + updateManager.downloadApk(showUpdate | !mUpdateMsg.getData().isForceType());
  195 + } else {
  196 + dialog1.dismiss();
  197 + }
  198 + }
  199 + }).create();// 创建
  200 + // 显示对话框
  201 + dialog.show();
  202 + }
  203 +
  204 + private void notNewVersionShow() {
  205 + String verName = getVerName(context);
  206 + String string = "当前版本" +
  207 + verName +
  208 + "已是最新版本";
  209 + Dialog dialog = new AlertDialog.Builder(context)
  210 + .setTitle("软件更新")
  211 + .setMessage(string)// 设置内容
  212 + .setPositiveButton("确定",// 设置确定按钮
  213 + new DialogInterface.OnClickListener() {
  214 + @Override
  215 + public void onClick(DialogInterface dialog1, int which) {
  216 + dialog1.dismiss();
  217 + }
  218 + }).create();// 创建
  219 + // 显示对话框
  220 + dialog.show();
  221 + }
  222 +
  223 +
  224 + private int getVerCode(Context context) {
  225 + int verCode = -1;
  226 + try {
  227 + verCode = context.getPackageManager().getPackageInfo(
  228 + packageName, 0).versionCode;
  229 + } catch (PackageManager.NameNotFoundException e) {
  230 + Log.e(TAG, e.getMessage());
  231 + }
  232 + return verCode;
  233 + }
  234 +
  235 + private String getVerName(Context context) {
  236 + String verName = "";
  237 + try {
  238 + verName = context.getPackageManager().getPackageInfo(
  239 + packageName, 0).versionName;
  240 + } catch (PackageManager.NameNotFoundException e) {
  241 + Log.e(TAG, e.getMessage());
  242 + }
  243 + return verName;
  244 +
  245 + }
  246 +}
... ...
  1 +/*
  2 + * Copyright (c) 2017. wugian
  3 + * Licensed under the Apache License, Version 2.0 (the "License");
  4 + * you may not use this file except in compliance with the License.
  5 + * You may obtain a copy of the License at
  6 + *
  7 + * http://www.apache.org/licenses/LICENSE-2.0
  8 + *
  9 + * Unless required by applicable law or agreed to in writing, software
  10 + * distributed under the License is distributed on an "AS IS" BASIS,
  11 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12 + * See the License for the specific language governing permissions and
  13 + * limitations under the License.
  14 + *
  15 + */
  16 +
  17 +package com.xgimi.gimicinema.appupdate;
  18 +
  19 +/**
  20 + * Created by wugian on 2017/4/25
  21 + */
  22 +public class UpdateMsg {
  23 +
  24 + /**
  25 + * code : 0
  26 + * msg :
  27 + * data : {"verCode":"12","verName":"v2.0","description":"1.3423423#2.ffffffffff","appUrl":"url","forceType":true}
  28 + */
  29 +
  30 + private int code;
  31 + private String msg;
  32 + private DataEntity data;
  33 +
  34 + public int getCode() {
  35 + return code;
  36 + }
  37 +
  38 + public void setCode(int code) {
  39 + this.code = code;
  40 + }
  41 +
  42 + public String getMsg() {
  43 + return msg;
  44 + }
  45 +
  46 + public void setMsg(String msg) {
  47 + this.msg = msg;
  48 + }
  49 +
  50 + public DataEntity getData() {
  51 + return data;
  52 + }
  53 +
  54 + public void setData(DataEntity data) {
  55 + this.data = data;
  56 + }
  57 +
  58 + public static class DataEntity {
  59 + /**
  60 + * verCode : 12
  61 + * verName : v2.0
  62 + * description : 1.3423423#2.ffffffffff
  63 + * appUrl : url
  64 + * forceType : true
  65 + */
  66 +
  67 + private int verCode;
  68 + private String verName;
  69 + private String description;
  70 + private String appUrl;
  71 + private boolean forceType;
  72 +
  73 + public int getVerCode() {
  74 + return verCode;
  75 + }
  76 +
  77 + public void setVerCode(int verCode) {
  78 + this.verCode = verCode;
  79 + }
  80 +
  81 + public String getVerName() {
  82 + return verName;
  83 + }
  84 +
  85 + public void setVerName(String verName) {
  86 + this.verName = verName;
  87 + }
  88 +
  89 + public String getDescription() {
  90 + return description;
  91 + }
  92 +
  93 + public void setDescription(String description) {
  94 + this.description = description;
  95 + }
  96 +
  97 + public String getAppUrl() {
  98 + return appUrl;
  99 + }
  100 +
  101 + public void setAppUrl(String appUrl) {
  102 + this.appUrl = appUrl;
  103 + }
  104 +
  105 + public boolean isForceType() {
  106 + return forceType;
  107 + }
  108 +
  109 + public void setForceType(boolean forceType) {
  110 + this.forceType = forceType;
  111 + }
  112 + }
  113 +}
... ...
  1 +/*
  2 + * Copyright (c) 2017. wugian
  3 + * Licensed under the Apache License, Version 2.0 (the "License");
  4 + * you may not use this file except in compliance with the License.
  5 + * You may obtain a copy of the License at
  6 + *
  7 + * http://www.apache.org/licenses/LICENSE-2.0
  8 + *
  9 + * Unless required by applicable law or agreed to in writing, software
  10 + * distributed under the License is distributed on an "AS IS" BASIS,
  11 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12 + * See the License for the specific language governing permissions and
  13 + * limitations under the License.
  14 + *
  15 + */
  16 +
  17 +package com.xgimi.gimicinema.appupdate;
  18 +
  19 +import android.app.AlertDialog;
  20 +import android.app.Dialog;
  21 +import android.content.Context;
  22 +import android.content.Intent;
  23 +import android.net.Uri;
  24 +import android.os.Environment;
  25 +import android.os.Handler;
  26 +import android.util.Log;
  27 +import android.widget.LinearLayout;
  28 +import android.widget.ProgressBar;
  29 +
  30 +import com.gimi.common.cinema.utils.OkHttpClientManager;
  31 +import com.gimi.common.cinema.utils.ShellUtils;
  32 +import com.squareup.okhttp.Request;
  33 +
  34 +import java.io.File;
  35 +
  36 +class UpgradeDownloadManager {
  37 + private Context mContext;
  38 + private String apkUrl;
  39 + private Dialog downloadDialog;
  40 + private ProgressBar mProgress;
  41 + private int progress;
  42 + private boolean silentInstall;
  43 +
  44 + UpgradeDownloadManager(Context context, String apkMessage, boolean silent) {
  45 + this.mContext = context;
  46 + this.apkUrl = apkMessage;
  47 + this.silentInstall = silent;
  48 + }
  49 +
  50 + private boolean hasSDCard() {
  51 + String status = Environment.getExternalStorageState();
  52 + return status.equals(Environment.MEDIA_MOUNTED);
  53 + }
  54 +
  55 + private String getRootFilePath() {
  56 + if (hasSDCard()) {
  57 + return Environment.getExternalStorageDirectory().getAbsolutePath()
  58 + + File.separator + File.separator + "test/"
  59 + /* + "android" + File.separator + "data" + File.separator*/;
  60 + } else {
  61 + return Environment.getDataDirectory().getAbsolutePath()
  62 + + File.separator + "data" + File.separator;
  63 + }
  64 + }
  65 +
  66 + void downloadApk(boolean showDialog) {
  67 + if (showDialog) {
  68 + AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
  69 + builder.setTitle("软件下载中.......");
  70 +
  71 + mProgress = new ProgressBar(mContext, null,
  72 + android.R.attr.progressBarStyleHorizontal);//水平条形
  73 + LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(200, -2);//长度200,高度默认
  74 + mProgress.setLayoutParams(lp2);
  75 + mProgress.setMax(100);
  76 + builder.setView(mProgress);
  77 + builder.setCancelable(false);
  78 + downloadDialog = builder.create();
  79 + downloadDialog.show();
  80 + }
  81 + downloadApk();
  82 + }
  83 +
  84 + private void downloadApk() {
  85 + String appName = apkUrl;
  86 + if (apkUrl.contains("/")) {
  87 + final String[] split = apkUrl.split("/");
  88 + appName = split[split.length - 1];
  89 + }
  90 + if (!new File(getRootFilePath()).exists()) {
  91 + final File file = new File(getRootFilePath());
  92 + file.mkdirs();
  93 + }
  94 + OkHttpClientManager.downloadAsyn(apkUrl, getRootFilePath() + appName,
  95 + new OkHttpClientManager.ResultCallback<String>() {
  96 + @Override
  97 + public void onError(Request request, Exception e) {
  98 +
  99 + }
  100 +
  101 + @Override
  102 + public void onResponse(String response) {
  103 + if (downloadDialog != null) {
  104 + downloadDialog.dismiss();
  105 + }
  106 + installApk(response);
  107 + }
  108 + }, new OkHttpClientManager.ProgressListener() {
  109 + @Override
  110 + public void onResponseProgress(long bytesRead, long contentLength) {
  111 + progress = (int) (((float) bytesRead / contentLength) * 100);
  112 + Log.d("install", "下载:" + progress + ".....");
  113 +
  114 + //更新进度
  115 + if (mProgress != null) {
  116 + mProgress.setProgress(progress);
  117 + }
  118 + }
  119 + });
  120 + }
  121 +
  122 + private void installApk(final String path) {
  123 + File apkFile = new File(path);
  124 + if (!apkFile.exists()) {
  125 + return;
  126 + }
  127 + new Handler().postDelayed(new Runnable() {
  128 + @Override
  129 + public void run() {
  130 + File apkFile = new File(path);
  131 + if (apkFile.exists()) {
  132 + boolean delete = apkFile.delete();
  133 + Log.d("install", delete + "");
  134 + }
  135 + }
  136 + }, 40 * 1000);
  137 +
  138 + if (silentInstall) {
  139 + Log.d("install", "正在静默安装");
  140 + ShellUtils.execCommand(new String[]{"su", "chmod -R 0777 " + path, "pm install -r " + path}, false);
  141 + return;
  142 + }
  143 + Log.d("install", "正在普通安装");
  144 + Intent intent = new Intent();
  145 + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  146 + intent.setAction(Intent.ACTION_VIEW);
  147 + Uri uri = Uri.fromFile(apkFile);
  148 + intent.setDataAndType(uri, "application/vnd.android.package-archive");
  149 + mContext.startActivity(intent);
  150 + }
  151 +}
\ No newline at end of file
... ...
... ... @@ -26,6 +26,7 @@ import com.gimi.common.cinema.utils.AppUpdateUtils;
26 26 import com.gimi.common.cinema.utils.Utils;
27 27 import com.xgimi.gimicinema.BuildConfig;
28 28 import com.xgimi.gimicinema.activity.CinemaConfig;
  29 +import com.xgimi.gimicinema.appupdate.AppUpgradeManager;
29 30
30 31 import java.io.File;
31 32 import java.io.FileInputStream;
... ... @@ -83,16 +84,19 @@ public class UpdateModelImpl implements IUpdateModel {
83 84
84 85 @Override
85 86 public void updateAppVersion(Context context, boolean bool) {
  87 +
  88 + final AppUpgradeManager appUpgradeManager = new AppUpgradeManager(context, "com.xgimi.gimicinema", bool);
86 89 // if (appUpdateUtils == null) {
87 90 // appUpdateUtils = new AppUpdateUtils(context);
88 91 // }
89 92 // appUpdateUtils.setShowNoUpdate(bool);
90   -// ThreadUtils.subThread(new ThreadUtils.DoSomeThing() {
91   -// @Override
92   -// public void doSomeThing() {
  93 + ThreadUtils.subThread(new ThreadUtils.DoSomeThing() {
  94 + @Override
  95 + public void doSomeThing() {
  96 + appUpgradeManager.updateAppVersion();
93 97 // appUpdateUtils.updateAppVersion();
94   -// }
95   -// });
  98 + }
  99 + });
96 100 }
97 101
98 102 @Override
... ...
Please register or login to post a comment