UpgradeDownloadManager.java 5.32 KB
/*
 *  Copyright (c)  2017.  wugian
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *  http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

package com.xgimi.gimicinema.appupdate;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

import com.gimi.common.cinema.utils.OkHttpClientManager;
import com.gimi.common.cinema.utils.ShellUtils;
import com.squareup.okhttp.Request;

import java.io.File;

class UpgradeDownloadManager {
    private Context mContext;
    private String apkUrl;
    private Dialog downloadDialog;
    private ProgressBar mProgress;
    private int progress;
    private boolean silentInstall;

    UpgradeDownloadManager(Context context, String apkMessage, boolean silent) {
        this.mContext = context;
        this.apkUrl = apkMessage;
        this.silentInstall = silent;
    }

    private boolean hasSDCard() {
        String status = Environment.getExternalStorageState();
        return status.equals(Environment.MEDIA_MOUNTED);
    }

    private String getRootFilePath() {
        if (hasSDCard()) {
            return Environment.getExternalStorageDirectory().getAbsolutePath()
                    + File.separator + File.separator + "test/"
                  /* + "android" + File.separator + "data" + File.separator*/;
        } else {
            return Environment.getDataDirectory().getAbsolutePath()
                    + File.separator + "data" + File.separator;
        }
    }

    void downloadApk(boolean showDialog) {
        if (showDialog) {
            AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
            builder.setTitle("软件下载中.......");

            mProgress = new ProgressBar(mContext, null,
                    android.R.attr.progressBarStyleHorizontal);//水平条形
            LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(200, -2);//长度200,高度默认
            mProgress.setLayoutParams(lp2);
            mProgress.setMax(100);
            builder.setView(mProgress);
            builder.setCancelable(false);
            downloadDialog = builder.create();
            downloadDialog.show();
        }
        downloadApk();
    }

    private void downloadApk() {
        String appName = apkUrl;
        if (apkUrl.contains("/")) {
            final String[] split = apkUrl.split("/");
            appName = split[split.length - 1];
        }
        if (!new File(getRootFilePath()).exists()) {
            final File file = new File(getRootFilePath());
            file.mkdirs();
        }
        OkHttpClientManager.downloadAsyn(apkUrl, getRootFilePath() + appName,
                new OkHttpClientManager.ResultCallback<String>() {
                    @Override
                    public void onError(Request request, Exception e) {

                    }

                    @Override
                    public void onResponse(String response) {
                        if (downloadDialog != null) {
                            downloadDialog.dismiss();
                        }
                        installApk(response);
                    }
                }, new OkHttpClientManager.ProgressListener() {
                    @Override
                    public void onResponseProgress(long bytesRead, long contentLength) {
                        progress = (int) (((float) bytesRead / contentLength) * 100);
                        Log.d("install", "下载:" + progress + ".....");

                        //更新进度
                        if (mProgress != null) {
                            mProgress.setProgress(progress);
                        }
                    }
                });
    }

    private void installApk(final String path) {
        File apkFile = new File(path);
        if (!apkFile.exists()) {
            return;
        }
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                File apkFile = new File(path);
                if (apkFile.exists()) {
                    boolean delete = apkFile.delete();
                    Log.d("install", delete + "");
                }
            }
        }, 40 * 1000);

        if (silentInstall) {
            Log.d("install", "正在静默安装");
            ShellUtils.execCommand(new String[]{"su", "chmod -R 0777 " + path, "pm install -r " + path}, false);
            return;
        }
        Log.d("install", "正在普通安装");
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction(Intent.ACTION_VIEW);
        Uri uri = Uri.fromFile(apkFile);
        intent.setDataAndType(uri, "application/vnd.android.package-archive");
        mContext.startActivity(intent);
    }
}