@@ -1 +0,0 @@ | |||
output-metadata.json |
@@ -5,7 +5,6 @@ plugins { | |||
android { | |||
namespace 'com.example.bpa' | |||
compileSdk 33 | |||
dataBinding { | |||
enabled = true | |||
} | |||
@@ -1,6 +1,8 @@ | |||
<?xml version="1.0" encoding="utf-8"?> | |||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |||
xmlns:tools="http://schemas.android.com/tools"> | |||
xmlns:tools="http://schemas.android.com/tools" | |||
android:revisionCode="1" | |||
android:versionName="1.0"> | |||
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> | |||
<uses-permission android:name="android.permission.INTERNET" /> | |||
@@ -20,6 +20,7 @@ import com.example.bpa.config.DataBus; | |||
import com.example.bpa.helper.MQTT; | |||
import com.example.bpa.helper.ModbusTcpHelper; | |||
import com.example.bpa.helper.ModbusTcpServer; | |||
import com.example.bpa.helper.UpdateManager; | |||
import com.example.bpa.service.OrderServer; | |||
import com.example.bpa.view.fragment.CloudFragment; | |||
import com.example.bpa.view.fragment.HeplerFragment; | |||
@@ -36,7 +37,8 @@ public class MainActivity extends FragmentActivity implements View.OnClickListen | |||
private ImageView CloudUpdates,SystemCapabilities,SystemSettings,SystemHelp,HomeMain,ColseMain; | |||
//页面 | |||
private FrameLayout fragment_container; | |||
//云端更新版本 | |||
private UpdateManager mUpdateManager; | |||
//endregion | |||
//region 界面 | |||
@@ -92,6 +94,7 @@ public class MainActivity extends FragmentActivity implements View.OnClickListen | |||
ShowFragment(homeFragment,"系统主页"); | |||
//ShowFragment(systemCapabilitiesFragment,"功能菜单"); | |||
DataBus.getInstance().UpdateMainGoods(); | |||
mUpdateManager = new UpdateManager(MainActivity.this); | |||
} | |||
/** | |||
* 初始化按钮事件 | |||
@@ -114,6 +117,14 @@ public class MainActivity extends FragmentActivity implements View.OnClickListen | |||
//MQTT数据监听 | |||
//OrderServer.Get().MqttInit(); | |||
} | |||
/** | |||
* 检测版本更新 | |||
*/ | |||
public void UpdateVersion() | |||
{ | |||
mUpdateManager.checkVersion(); | |||
} | |||
//endregion | |||
//region 点击事件 | |||
@@ -126,7 +137,7 @@ public class MainActivity extends FragmentActivity implements View.OnClickListen | |||
public void onClick(View v) { | |||
switch (v.getId()) { | |||
case R.id.CloudUpdates://系统更新按钮 | |||
ShowFragment(cloudFragment,"云端更新"); | |||
UpdateVersion(); | |||
break; | |||
case R.id.SystemCapabilities://系统功能按钮 | |||
ShowFragment(systemCapabilitiesFragment,"功能菜单"); | |||
@@ -8,6 +8,7 @@ import com.example.bpa.db.mode.BPA_SYSTEMSET; | |||
import com.example.bpa.db.mode.BPA_USER; | |||
import com.example.bpa.helper.ConfigUtil; | |||
import com.example.bpa.helper.T; | |||
import com.example.bpa.helper.UpdateManager; | |||
import java.util.ArrayList; | |||
@@ -0,0 +1,305 @@ | |||
package com.example.bpa.helper; | |||
import android.app.AlertDialog; | |||
import android.content.Context; | |||
import android.content.Intent; | |||
import android.net.Uri; | |||
import android.os.Environment; | |||
import android.os.Handler; | |||
import android.os.Message; | |||
import android.util.Log; | |||
import android.view.View; | |||
import android.view.Window; | |||
import android.widget.Button; | |||
import android.widget.ProgressBar; | |||
import android.widget.TextView; | |||
import com.example.bpa.R; | |||
import com.example.bpa.config.ConfigName; | |||
import org.json.JSONObject; | |||
import java.io.File; | |||
import java.io.FileNotFoundException; | |||
import java.io.FileOutputStream; | |||
import java.io.IOException; | |||
import java.io.InputStream; | |||
import java.lang.ref.WeakReference; | |||
import java.net.HttpURLConnection; | |||
import java.net.MalformedURLException; | |||
import java.net.URL; | |||
/* | |||
* 检验apk是否有新版本,是否升级 | |||
* @author fengyoufu 20230518 | |||
*/ | |||
public class UpdateManager { | |||
/* 上下文 */ | |||
private Context mContext; | |||
/* 下载包安装路径 */ | |||
private static final String savePath = ConfigName.getInstance().appResRoot+"/Download/"; | |||
private static final int NEW_VERSION = 3; | |||
private static final int DOWN_UPDATE = 1; | |||
private static final int DOWN_OVER = 2; | |||
/* 刷新的进度条 */ | |||
private ProgressBar mProgress; | |||
/* 新版本apk的远程路径 */ | |||
private String apkUrl ; | |||
/* 保存下载的apk的名称为 */ | |||
private String saveFileName; | |||
/* 下载进度 */ | |||
private int progress; | |||
private boolean interceptFlag = false; | |||
/* | |||
* Handler静态化,防止内存泄露 | |||
*/ | |||
private static class MHandler extends Handler | |||
{ | |||
/* 引用外部类 */ | |||
private WeakReference<UpdateManager> updatemanager; | |||
public MHandler(UpdateManager activity) | |||
{ | |||
updatemanager = new WeakReference<UpdateManager>(activity); | |||
} | |||
/* 处理线程结果 */ | |||
@Override | |||
public void handleMessage(android.os.Message msg) | |||
{ | |||
UpdateManager theClass = updatemanager.get(); | |||
switch (msg.what) | |||
{ | |||
//对比后有新版本 | |||
case NEW_VERSION: | |||
JSONObject jsonObject=(JSONObject)msg.obj; | |||
theClass.showUpdateDialog(jsonObject); | |||
break; | |||
//正在下载,更新进度条 | |||
case DOWN_UPDATE: | |||
theClass.showProgress(); | |||
break; | |||
//下载完成,进行安装 | |||
case DOWN_OVER: | |||
theClass.installApk(); | |||
} | |||
} | |||
} | |||
/* 实例化Handler */ | |||
MHandler mHandler = new MHandler(this); | |||
//构造函数 | |||
public UpdateManager(Context context) { | |||
this.mContext = context; | |||
} | |||
/* | |||
* 检查新版本 | |||
*/ | |||
public void checkVersion() | |||
{ | |||
android.util.Log.i("UpdateManager","checkVersion"); | |||
Runnable checkVersionRunnable = new Runnable(){ | |||
@Override | |||
public void run() | |||
{ | |||
try | |||
{ | |||
//远程请求服务器,获取远程服务器上的apk版本;这里我们直接模拟返回的结果 | |||
JSONObject data = new JSONObject("{" | |||
+ "\"status\":1," | |||
+ "\"msg\":{" | |||
+ "\"versionCode\":2," | |||
+ "\"versionName\":\"1.1.7\"," | |||
+ "\"versionUrl\":\"http://124.222.238.75/blnc.apk\"," | |||
+ "\"versionDesc\":\"1,新增定位功能;2,修复定位功能相关BUG;3,增加针对信息推送功能;4,增加针对信息推送功能;5,增加针对信息推送功能;6,增加针对信息推送功能;7,增加针对信息推送功能;\"," | |||
+ "\"versionDate\":\"2013.11.48\"," | |||
+ "\"versionSize\":\"1.6M\"}}"); | |||
if(data.length()>0) //有返回数据,则发送消息 | |||
{ | |||
int status = data.getInt("status"); | |||
if(status==1) | |||
{ | |||
JSONObject newVersionInfo = data.getJSONObject("msg"); | |||
int versionCode = newVersionInfo.getInt("versionCode"); | |||
String versionName = newVersionInfo.getString("versionName"); | |||
saveFileName = "hlnc_"+versionName+".apk"; | |||
apkUrl = newVersionInfo.getString("versionUrl"); | |||
int versionCodeClient = Utils.getversionCode(mContext); | |||
if(versionCode>versionCodeClient) //服务器版本大于本机版本,则视为有新版本 | |||
{ | |||
Message message = mHandler.obtainMessage(NEW_VERSION); | |||
message.obj = newVersionInfo; | |||
mHandler.sendMessage(message); | |||
}else | |||
{ | |||
T.show(mContext,"当前无更新内容!"); | |||
} | |||
}else | |||
{ | |||
T.show(mContext,"当前无更新内容!"); | |||
} | |||
}else | |||
{ | |||
T.show(mContext,"当前无更新内容!"); | |||
} | |||
} | |||
catch(Exception e) | |||
{ | |||
android.util.Log.i("UpdateManager",e.toString()); | |||
} | |||
} | |||
}; | |||
new Thread(checkVersionRunnable).start();; | |||
} | |||
/* 显示进度条 | |||
* 外部类进行封装以便MHandler进行内部调用 | |||
*/ | |||
public void showProgress() | |||
{ | |||
mProgress.setProgress(progress); | |||
} | |||
/* | |||
* 显示版本更新提示框 | |||
*/ | |||
private void showUpdateDialog(JSONObject newVersionInfo){ | |||
final AlertDialog alg = new AlertDialog.Builder(mContext).create(); | |||
alg.show(); | |||
Window win = alg.getWindow(); | |||
win.setContentView(R.layout.common_dialog_update); | |||
//显示新版本内容 | |||
TextView title_update = (TextView)win.findViewById(R.id.title_update); | |||
TextView title_date = (TextView)win.findViewById(R.id.title_date); | |||
TextView title_size = (TextView)win.findViewById(R.id.title_size); | |||
TextView versionDesc = (TextView)win.findViewById(R.id.versionDesc); | |||
try{ | |||
title_update.setText(newVersionInfo.getString("versionName")+"版本升级提示"); | |||
title_date.setText("发布时间:"+newVersionInfo.getString("versionDate")); | |||
title_size.setText("大小:"+newVersionInfo.getString("versionSize")); | |||
//提示内容按";"进行整合,这里拆分到数组中 | |||
String desc= newVersionInfo.getString("versionDesc").replace(";","\n"); | |||
versionDesc.setText(desc); | |||
Button positiveButton = (Button)win.findViewById(R.id.PositiveButton); | |||
Button negativeButton = (Button)win.findViewById(R.id.NegativeButton); | |||
positiveButton.setOnClickListener(new View.OnClickListener() { | |||
@Override | |||
public void onClick(View v) { | |||
// TODO Auto-generated method stub | |||
alg.dismiss(); | |||
showDownloadDialog(); | |||
} | |||
}); | |||
negativeButton.setOnClickListener(new View.OnClickListener() { | |||
@Override | |||
public void onClick(View v) { | |||
// TODO Auto-generated method stub | |||
alg.dismiss(); | |||
} | |||
}); | |||
}catch(Exception e){ | |||
e.printStackTrace(); | |||
} | |||
} | |||
/* | |||
* 显示下载中界面 | |||
*/ | |||
private void showDownloadDialog(){ | |||
final AlertDialog alg = new AlertDialog.Builder(mContext).create(); | |||
alg.show(); | |||
Window win = alg.getWindow(); | |||
win.setContentView(R.layout.common_dialog_progress); | |||
mProgress = (ProgressBar)win.findViewById(R.id.update_progress); | |||
Button negativeButton = (Button)win.findViewById(R.id.NegativeButton); | |||
negativeButton.setOnClickListener(new View.OnClickListener() { | |||
@Override | |||
public void onClick(View v) { | |||
// TODO Auto-generated method stub | |||
alg.dismiss(); | |||
interceptFlag = true; //中止下载 | |||
} | |||
}); | |||
downloadApk(); | |||
} | |||
/* | |||
* 下载apk | |||
* @param url | |||
*/ | |||
private void downloadApk(){ | |||
Runnable mdownApkRunnable = new Runnable() { | |||
@Override | |||
public void run() { | |||
try { | |||
URL url = new URL(apkUrl); | |||
HttpURLConnection conn = (HttpURLConnection)url.openConnection(); | |||
conn.setRequestProperty("Accept-Encoding", "identity"); | |||
conn.connect(); | |||
int length = conn.getContentLength(); | |||
InputStream is = conn.getInputStream(); | |||
File file = new File(savePath); | |||
if (!file.exists()) //创建目录 | |||
{ | |||
file.mkdirs(); | |||
} | |||
File ApkFile = new File(file,saveFileName); | |||
FileOutputStream fos = new FileOutputStream(ApkFile); | |||
int count = 0; | |||
byte buf[] = new byte[1024]; | |||
do{ | |||
int numread = is.read(buf); | |||
count += numread; | |||
progress =(int)(((float)count / length) * 100); | |||
//更新进度 | |||
mHandler.sendEmptyMessage(DOWN_UPDATE); | |||
if(numread <= 0){ | |||
//下载完成通知安装 | |||
mHandler.sendEmptyMessage(DOWN_OVER); | |||
T.show(mContext,"下载完成,正在安装..."); | |||
break; | |||
} | |||
fos.write(buf,0,numread); | |||
}while(!interceptFlag); //点击取消就停止下载. | |||
fos.close(); | |||
is.close(); | |||
} catch (MalformedURLException e) { | |||
android.util.Log.i("MalformedURLException",e.toString()); | |||
} catch(IOException e){ | |||
android.util.Log.i("IOException",e.toString()); | |||
} | |||
} | |||
}; | |||
new Thread(mdownApkRunnable).start(); | |||
} | |||
/* | |||
* 安装apk | |||
* @param url | |||
*/ | |||
private void installApk(){ | |||
File apkfile = new File(savePath+saveFileName); | |||
if (!apkfile.exists()) { | |||
return; | |||
} | |||
Intent i = new Intent(Intent.ACTION_VIEW); | |||
i.setDataAndType(Uri.parse("file://" + apkfile.toString()), "application/vnd.android.package-archive"); | |||
mContext.startActivity(i); | |||
} | |||
} |
@@ -0,0 +1,23 @@ | |||
package com.example.bpa.helper; | |||
import android.content.Context; | |||
/** | |||
* 获取当前的包版本 | |||
*/ | |||
public class Utils { | |||
/* | |||
* 获取当前app的versionCode | |||
* return int | |||
*/ | |||
public static int getversionCode(Context context) { | |||
int verCode = -1; | |||
try { | |||
verCode = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode; | |||
} catch (Exception e) { | |||
android.util.Log.e("mUtils-gerversionCode", e.getMessage()); | |||
} | |||
return verCode; | |||
} | |||
} |
@@ -10,17 +10,50 @@ import android.view.LayoutInflater; | |||
import android.view.View; | |||
import android.view.ViewGroup; | |||
import com.example.bpa.MainActivity; | |||
import com.example.bpa.R; | |||
import com.example.bpa.helper.UpdateManager; | |||
import com.example.bpa.view.fragment.setting.mqttparameter; | |||
import com.example.bpa.view.fragment.setting.ovarparameter; | |||
import com.example.bpa.view.fragment.setting.systemparameter; | |||
/** | |||
* 上传云端界面 | |||
*/ | |||
public class CloudFragment extends Fragment { | |||
public class CloudFragment extends Fragment implements View.OnClickListener{ | |||
View view; | |||
@Nullable | |||
@Override | |||
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { | |||
View view = inflater.inflate(R.layout.fragment_cloud, container,false); | |||
view = inflater.inflate(R.layout.fragment_cloud, container,false); | |||
Init(); | |||
initEvent(); | |||
return view; | |||
} | |||
private void Init() { | |||
} | |||
private void initEvent() { | |||
} | |||
@Override | |||
public void onClick(View v) { | |||
switch (v.getId()) { | |||
case R.id.stparameter://系統參數設置 | |||
break; | |||
case R.id.mqttparameter://MQTT配置 | |||
break; | |||
case R.id.overparameter://其他设置 | |||
break; | |||
} | |||
} | |||
} |
@@ -0,0 +1,6 @@ | |||
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > | |||
<item android:id="@android:id/background" android:drawable="@drawable/common_progress_bar_background"/> | |||
<item android:id="@android:id/progress" android:drawable="@drawable/common_progress_bar_progress"/> | |||
</layer-list> |
@@ -0,0 +1,21 @@ | |||
<?xml version="1.0" encoding="UTF-8"?> | |||
<!-- 不带圆角 白色背景 椙栏色边框 下边框 长方体 --> | |||
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > | |||
<item> | |||
<shape> | |||
<solid android:color="#FFFFFF" /> | |||
<stroke | |||
android:width="3dp" | |||
android:color="#01c3ed" /> | |||
</shape> | |||
</item> | |||
<item | |||
android:bottom="3dp"> | |||
<shape> | |||
<solid android:color="#FFFFFF" /> | |||
<stroke | |||
android:width="1dp" | |||
android:color="#ffffffff" /> | |||
</shape> | |||
</item> | |||
</layer-list> |
@@ -0,0 +1,39 @@ | |||
<?xml version="1.0" encoding="utf-8"?> | |||
<LinearLayout | |||
xmlns:android="http://schemas.android.com/apk/res/android" | |||
android:layout_width="match_parent" | |||
android:layout_height="wrap_content" | |||
android:orientation="vertical" | |||
android:paddingLeft="10.0dip" | |||
android:paddingRight="10.0dip" | |||
android:paddingBottom="15.0dip" | |||
android:background="#fff"> | |||
<TextView | |||
android:layout_width="match_parent" | |||
android:layout_height="45.0dip" | |||
android:gravity="center_vertical" | |||
android:layout_marginBottom="5.0dip" | |||
android:text="正在下载新版本"/> | |||
<ProgressBar | |||
android:id="@+id/update_progress" | |||
android:layout_width="match_parent" | |||
android:layout_height="10.0dip" | |||
style="?android:attr/progressBarStyleHorizontal" | |||
android:max="100" | |||
android:progress="0" | |||
/> | |||
<Button | |||
android:id="@+id/NegativeButton" | |||
android:layout_width="match_parent" | |||
android:layout_height="0dip" | |||
android:layout_weight="1" | |||
android:text="取消下载" | |||
android:layout_marginTop="10.0dip" | |||
android:paddingTop="10.0dip" | |||
android:paddingBottom="10.0dip" | |||
android:background="@drawable/btn_greenblue" /> | |||
</LinearLayout> |
@@ -0,0 +1,81 @@ | |||
<?xml version="1.0" encoding="utf-8"?> | |||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |||
android:layout_width="wrap_content" | |||
android:layout_height="wrap_content" | |||
android:orientation="vertical" | |||
android:background="#ffffff"> | |||
<LinearLayout android:layout_width="match_parent" | |||
android:layout_height="wrap_content" | |||
android:orientation="vertical" | |||
android:background="@drawable/shape_bottom_blue" | |||
android:padding="10dip"> | |||
<TextView android:id="@+id/title_update" | |||
android:layout_width="match_parent" | |||
android:layout_height="wrap_content" | |||
android:gravity="center_vertical" | |||
android:textColor="#333333" | |||
android:textSize="18sp"/> | |||
<RelativeLayout android:layout_width="match_parent" | |||
android:layout_height="wrap_content" | |||
android:orientation="horizontal"> | |||
<TextView android:id="@+id/title_date" | |||
android:layout_width="wrap_content" | |||
android:layout_height="wrap_content" | |||
android:textColor="#999999" | |||
android:layout_alignParentLeft="true"/> | |||
<TextView android:id="@+id/title_size" | |||
android:layout_width="wrap_content" | |||
android:layout_height="wrap_content" | |||
android:textColor="#999999" | |||
android:layout_alignParentRight="true"/> | |||
</RelativeLayout> | |||
</LinearLayout> | |||
<LinearLayout | |||
android:layout_width="match_parent" | |||
android:layout_height="wrap_content" | |||
android:orientation="vertical" | |||
android:paddingTop="5dip" | |||
android:paddingLeft="10.0dip" | |||
android:paddingRight="10.0dip" | |||
android:paddingBottom="5.0dip" | |||
android:background="#ffffff"> | |||
<TextView | |||
android:id="@+id/versionDesc" | |||
android:layout_width="match_parent" | |||
android:layout_height="wrap_content" | |||
android:gravity="center_vertical" | |||
android:textColor="#666666"/> | |||
<LinearLayout | |||
android:layout_width="match_parent" | |||
android:layout_height="40.0dip" | |||
android:gravity="center" | |||
android:layout_marginTop="0dip"> | |||
<Button | |||
android:id="@+id/PositiveButton" | |||
android:layout_width="match_parent" | |||
android:layout_height="40.0dip" | |||
android:layout_weight="1" | |||
android:text="确认升级" | |||
android:layout_marginRight="5.0dip" | |||
android:textSize="16sp" | |||
android:background="@drawable/btn_greenblue"/> | |||
<Button | |||
android:id="@+id/NegativeButton" | |||
android:layout_width="match_parent" | |||
android:layout_height="40.0dip" | |||
android:layout_weight="1" | |||
android:text="暂不升级" | |||
android:layout_marginLeft="5.0dip" | |||
android:textSize="16sp" | |||
android:background="@drawable/btn_greenblue" /> | |||
</LinearLayout> | |||
</LinearLayout> | |||
</LinearLayout> |
@@ -18,4 +18,5 @@ android.useAndroidX=true | |||
# Enables namespacing of each library's R class so that its R class includes only the | |||
# resources declared in the library itself and none from the library's dependencies, | |||
# thereby reducing the size of the R class for that library | |||
android.nonTransitiveRClass=true | |||
android.nonTransitiveRClass=true | |||
#android.enableJetifier=true |