package com.mjsheng.myappstore.server;

/**
 * 作者 mjsheng
 * 日期 2019/4/1 10:58
 * 邮箱 501802639@qq.com
 * 来自：
 */

import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

import com.arialyy.annotations.Download;
import com.arialyy.aria.core.Aria;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.mjsheng.myappstore.KeepAliveConnection;
import com.mjsheng.myappstore.utils.ApkUtils;
import com.mjsheng.myappstore.utils.ServiceAliveUtils;


/**
 * 守护进程 双进程通讯
 *
 * @author LiGuangMin
 * @time Created by 2018/8/17 11:27
 */
public class GuardService extends Service {
    private final static String TAG = GuardService.class.getSimpleName();
    private ServiceConnection mServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            LogUtils.e(TAG, "GuardService:建立链接");
            boolean isServiceRunning = ServiceAliveUtils.isServiceAlice();
            if (!isServiceRunning) {
                Intent i = new Intent(GuardService.this, MyDownloadService.class);
                startService(i);
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            // 断开链接
            startService(new Intent(GuardService.this, StepService.class));
            // 重新绑定
            bindService(new Intent(GuardService.this, StepService.class), mServiceConnection, Context.BIND_IMPORTANT);
        }
    };

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return new KeepAliveConnection.Stub() {
        };
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {


        Aria.init(this);
        Aria.get(this).getDownloadConfig().setMaxTaskNum(1);


        Aria.download(this).register();





        //        startForeground(1, new Notification());
        // 绑定建立链接
        bindService(new Intent(this, StepService.class), mServiceConnection, Context.BIND_IMPORTANT);
        return START_STICKY;
    }
    //在这里处理任务执行中的状态，如进度进度条的刷新
    @Download.onTaskRunning
    protected void running(com.arialyy.aria.core.download.DownloadTask task) {
        Log.e("mjsheng", "正在下载=--------------::" + task.getState() + "-------" + task.getPercent()+ "-------" + task.getExtendField());
        ToastUtils.showShort("正在下载=--------------::" + task.getExtendField() + "-------" + task.getPercent());
    }

    @Download.onTaskComplete
    void taskComplete(com.arialyy.aria.core.download.DownloadTask task) {
        //在这里处理任务完成的状态
        String filepath = task.getDownloadPath();
        String packageName = task.getExtendField();
        Log.e("mjsheng", "downloadPath::" + filepath);
        Log.e("mjsheng", "extendField::" + packageName);
        ApkUtils.installApkInSilence(filepath, packageName);
    }
}

