|
|
@@ -1,331 +0,0 @@
|
|
|
-package com.kfzs.duanduan.oem;
|
|
|
-
|
|
|
-import android.app.DownloadManager;
|
|
|
-import android.content.BroadcastReceiver;
|
|
|
-import android.content.Context;
|
|
|
-import android.content.Intent;
|
|
|
-import android.content.IntentFilter;
|
|
|
-import android.database.Cursor;
|
|
|
-import android.net.Uri;
|
|
|
-import android.os.Environment;
|
|
|
-import android.os.Handler;
|
|
|
-import android.os.Message;
|
|
|
-import android.text.TextUtils;
|
|
|
-import android.util.Log;
|
|
|
-import android.view.Gravity;
|
|
|
-import android.webkit.JavascriptInterface;
|
|
|
-import android.webkit.WebView;
|
|
|
-import android.widget.Toast;
|
|
|
-
|
|
|
-import com.kfzs.duanduan.datashare.AppDownloadDatabaseHelper;
|
|
|
-import com.kfzs.duanduan.datashare.provider.appdownload.AppdownloadBean;
|
|
|
-
|
|
|
-import java.io.File;
|
|
|
-import java.io.IOException;
|
|
|
-import java.net.HttpURLConnection;
|
|
|
-import java.util.Random;
|
|
|
-
|
|
|
-import static android.content.Intent.ACTION_PACKAGE_ADDED;
|
|
|
-
|
|
|
-/**
|
|
|
- * App下载帮助页面
|
|
|
- * Created by Administrator on 2017/12/14.
|
|
|
- */
|
|
|
-
|
|
|
-public class DownLoadAppUtil {
|
|
|
-
|
|
|
- public static DownLoadAppUtil downLoadAppUtil;
|
|
|
- private static Context mContext;
|
|
|
- BroadcastReceiver receiver;
|
|
|
-
|
|
|
- public static DownLoadAppUtil getInstance(Context context){
|
|
|
- mContext = context;
|
|
|
- if(null == downLoadAppUtil){
|
|
|
- downLoadAppUtil = new DownLoadAppUtil();
|
|
|
- }
|
|
|
- return downLoadAppUtil;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- public class JsInteraction {
|
|
|
- @JavascriptInterface
|
|
|
- public void getNameIcon(String name, String icon, String url) { //提供给js调用的方法
|
|
|
- Log.e("js_interaction---",name+"");
|
|
|
- Toast.makeText(mContext.getApplicationContext(), name, Toast.LENGTH_LONG).show();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public void downloadApp(WebView mWebView, AppdownloadBean appDownloadBean, DownloadManager downloadManager){
|
|
|
- if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
|
|
|
- Toast toast = Toast.makeText(mContext, "需要SD卡。", Toast.LENGTH_LONG);
|
|
|
- toast.setGravity(Gravity.CENTER, 0, 0);
|
|
|
- toast.show();
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- if(appDownloadBean == null ){
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- DownloadManager.Request request = new DownloadManager.Request(Uri.parse(appDownloadBean.getUrl()));
|
|
|
- request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);
|
|
|
- request.setDestinationInExternalFilesDir(mContext,Environment.DIRECTORY_DOWNLOADS, appDownloadBean.getName()+"");
|
|
|
- request.allowScanningByMediaScanner();
|
|
|
- request.setVisibleInDownloadsUi(true);
|
|
|
- request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
|
|
|
- reference = downloadManager.enqueue(request);
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 下载完成后通知
|
|
|
- */
|
|
|
- public void initFinishRecicever(BroadcastReceiver receiver, final DownloadManager downloadManager){
|
|
|
-
|
|
|
- IntentFilter intentFilter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
|
|
|
- receiver = new BroadcastReceiver() {
|
|
|
- @Override
|
|
|
- public void onReceive(Context context, Intent intent) {
|
|
|
- long references = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
|
|
|
- initDownLoadUri(references ,downloadManager);
|
|
|
-
|
|
|
- }
|
|
|
- };
|
|
|
- mContext.registerReceiver(receiver, intentFilter);
|
|
|
-
|
|
|
- }
|
|
|
- /**
|
|
|
- * 安装完成后通知
|
|
|
- */
|
|
|
- public void installFinishRecicever(BroadcastReceiver receiver, final DownloadManager downloadManager){
|
|
|
- IntentFilter intentFilter = new IntentFilter();
|
|
|
-// intentFilter.s(Intent.ACTION_PACKAGE_ADDED);
|
|
|
- receiver = new BroadcastReceiver() {
|
|
|
- @Override
|
|
|
- public void onReceive(Context context, Intent intent) {
|
|
|
-
|
|
|
- if(intent.getAction().equals(ACTION_PACKAGE_ADDED)) {
|
|
|
- String packages = intent.getDataString();
|
|
|
- String packageName = intent.getPackage();
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
- };
|
|
|
- mContext.registerReceiver(receiver, intentFilter);
|
|
|
- }
|
|
|
- /**
|
|
|
- * 用户查询文件下载地址的索引
|
|
|
- */
|
|
|
- private Cursor myDownload;
|
|
|
- private String statusMsg;
|
|
|
- long reference;
|
|
|
- String appName = "";
|
|
|
- public synchronized void initDownLoadUri(long re, DownloadManager downloadManager){
|
|
|
- DownloadManager.Query query = new DownloadManager.Query();
|
|
|
- query.setFilterById(re);
|
|
|
- myDownload = downloadManager.query(query);
|
|
|
- if(myDownload.moveToFirst()){
|
|
|
- int fileNameIdx = myDownload.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME);
|
|
|
- int fileUriIdx = myDownload.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI);
|
|
|
- //文件名
|
|
|
- final String fileName = myDownload.getString(fileNameIdx);
|
|
|
- //文件地址
|
|
|
- String fileUrl = myDownload.getString(fileUriIdx);
|
|
|
-
|
|
|
- Log.e("fileName----fileUrl","fileName:"+fileName+",fileUrl:"+fileUrl);
|
|
|
- //当前状态
|
|
|
- int status = myDownload.getInt(myDownload.getColumnIndex(DownloadManager.COLUMN_STATUS));
|
|
|
- switch (status){
|
|
|
- case DownloadManager.STATUS_PAUSED:
|
|
|
- statusMsg = "STATUS_PAUSED";
|
|
|
- break;
|
|
|
- case DownloadManager.STATUS_PENDING:
|
|
|
- statusMsg = "STATUS_PENDING";
|
|
|
- break;
|
|
|
- case DownloadManager.STATUS_RUNNING:
|
|
|
- statusMsg = "STATUS_RUNNING";
|
|
|
- break;
|
|
|
- case DownloadManager.STATUS_SUCCESSFUL:
|
|
|
- statusMsg = "STATUS_SUCCESSFUL";
|
|
|
- //获取文件 "file://"
|
|
|
- String startPath = Environment.getExternalStorageDirectory().getAbsolutePath()
|
|
|
- + File.separator
|
|
|
- + "Android"
|
|
|
- + File.separator
|
|
|
- + "data"
|
|
|
- + File.separator
|
|
|
- + mContext.getPackageName()
|
|
|
- + File.separator
|
|
|
- + "files"
|
|
|
- + File.separator;
|
|
|
- String pks = GetPackageNmeHlpe.getInstance().getPackageNames(mContext, fileName);
|
|
|
- if(TextUtils.isEmpty(pks)){
|
|
|
- return;
|
|
|
- }
|
|
|
- final AppdownloadBean appdownloadBean = AppDownloadDatabaseHelper.getInstance().getApps(mContext, pks);
|
|
|
- if(appdownloadBean != null){
|
|
|
-
|
|
|
- try {
|
|
|
- new Thread() {
|
|
|
- @Override
|
|
|
- public void run() {
|
|
|
- try {
|
|
|
- appdownloadBean.setStaus(1);
|
|
|
-// String packageName = appdownloadBean.getPkgName(),
|
|
|
-// interfaceName = appdownloadBean.getInterfaceName(),
|
|
|
-// staus = 1 +"";
|
|
|
-// String url_data = "http://testten.kuaifazs.com:7777/recall?packageName="+ URLEncoder.encode(packageName,"utf8")+"&interfaceName="+interfaceName+"&status="+staus+ UtilParamsWebview.getInstance().getParams(mContext);
|
|
|
- UtilParamsWebview.getInstance().returnBitMap(AppDownloadUtil.getInstance().accessInterfaceNetwork(mContext, AppDownloadUtil.URL, appdownloadBean));
|
|
|
-
|
|
|
- installFile(fileName);
|
|
|
- AppDownloadDatabaseHelper.getInstance().setApps(mContext ,appdownloadBean);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
- }.start();
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
-
|
|
|
-// mWebView.loadUrl("http://testten.kuaifazs.com/?t=1" + UtilParamsWebview.getInstance().getParams(mContext));
|
|
|
- break;
|
|
|
- }
|
|
|
- break;
|
|
|
- case DownloadManager.STATUS_FAILED:
|
|
|
- statusMsg = "STATUS_FAILED";
|
|
|
- break;
|
|
|
- default:
|
|
|
- statusMsg = "unknow status";
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 下载过程点击通知
|
|
|
- */
|
|
|
- public void initNotificationClickReceiver(BroadcastReceiver clickedReceiver, final DownloadManager downloadManager) {
|
|
|
- IntentFilter intentFilter = new IntentFilter(DownloadManager.ACTION_NOTIFICATION_CLICKED);
|
|
|
- clickedReceiver = new BroadcastReceiver() {
|
|
|
- @Override
|
|
|
- public void onReceive(Context context, Intent intent) {
|
|
|
- String pN = intent.getStringExtra("packge_name");
|
|
|
- long clickReferences = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
|
|
|
- String extraId = DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS;
|
|
|
- long[] references = intent.getLongArrayExtra(extraId);
|
|
|
- for (long refer : references) {
|
|
|
- if (refer == clickReferences) {
|
|
|
- initDownLoadUri(refer ,downloadManager);
|
|
|
- if("STATUS_SUCCESSFUL".equals(statusMsg)) {
|
|
|
-// installFile();
|
|
|
- }else{
|
|
|
- Toast.makeText(mContext, "下载还未完成", Toast.LENGTH_SHORT).show();
|
|
|
- }
|
|
|
- myDownload.close();
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- };
|
|
|
- mContext.registerReceiver(clickedReceiver, intentFilter);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- /**
|
|
|
- * 跳转到安装界面--安装文件
|
|
|
- */
|
|
|
- private synchronized void installFile(String fileName) {
|
|
|
- //跳转到安装界面
|
|
|
- Intent i = new Intent();
|
|
|
- i.setAction(Intent.ACTION_VIEW);
|
|
|
- i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
-
|
|
|
- Log.e("fileName----appname","fileName:"+fileName+",appname:"+appName);
|
|
|
- if(fileName == null){
|
|
|
- return;
|
|
|
- }
|
|
|
- i.setDataAndType(Uri.parse("file://" + fileName), "application/vnd.android.package-archive");
|
|
|
- mContext.startActivity(i);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * test
|
|
|
- */
|
|
|
- public void sendRequest(final AppdownloadBean appdownloadBean){
|
|
|
- try{
|
|
|
- if(appdownloadBean == null){
|
|
|
- return;
|
|
|
- }
|
|
|
- Message message = mHandler.obtainMessage();
|
|
|
- message.what = 1;
|
|
|
- message.obj = appdownloadBean;
|
|
|
- mHandler.sendMessageDelayed(message, RandomNum() * 1000);
|
|
|
-
|
|
|
- }catch (Exception e){
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 生成随机数
|
|
|
- */
|
|
|
- private int RandomNum(){
|
|
|
- int min = 5;
|
|
|
- int max = 9;
|
|
|
- Random random = new Random();
|
|
|
- int randomNum = random.nextInt(max)%(max -min + 1) +min;
|
|
|
- Log.e("--------", randomNum+"");
|
|
|
- return randomNum;
|
|
|
- }
|
|
|
-
|
|
|
- Handler mHandler = new Handler(){
|
|
|
- @Override
|
|
|
- public void handleMessage(Message msg) {
|
|
|
- super.handleMessage(msg);
|
|
|
- switch (msg.what){
|
|
|
- case 0: // 安装成功请求
|
|
|
- try{
|
|
|
- if(msg.obj instanceof AppdownloadBean){
|
|
|
- AppdownloadBean appdownloadBean = (AppdownloadBean) msg.obj;
|
|
|
- if(appdownloadBean == null){
|
|
|
- return;
|
|
|
- }
|
|
|
- appdownloadBean.setStaus(2);
|
|
|
- UtilParamsWebview.getInstance().returnBitMap(AppDownloadUtil.getInstance().accessInterfaceNetwork(mContext, AppDownloadUtil.URL, appdownloadBean));
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- }catch (Exception e){
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- break;
|
|
|
- case 1://下载成功请求
|
|
|
- try {
|
|
|
- if(msg.obj instanceof AppdownloadBean){
|
|
|
- AppdownloadBean appdownloadBean = (AppdownloadBean) msg.obj;
|
|
|
- if(appdownloadBean == null){
|
|
|
- return;
|
|
|
- }
|
|
|
- appdownloadBean.setStaus(1);
|
|
|
- final HttpURLConnection connection = UtilParamsWebview.getInstance().returnBitMap(AppDownloadUtil.getInstance().accessInterfaceNetwork(mContext, AppDownloadUtil.URL, appdownloadBean));
|
|
|
- try {
|
|
|
- if(connection != null && connection.getResponseCode() == 200){
|
|
|
- Message message = mHandler.obtainMessage();
|
|
|
- message.what = 0;
|
|
|
- message.obj = appdownloadBean;
|
|
|
- mHandler.sendMessageDelayed(msg, RandomNum() * 1 * 1000);
|
|
|
- }
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
- }catch (Exception e){
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- };
|
|
|
-}
|