lmw
2023-06-06 7a563b559c48b9b339784c25fc5f0adc2ab5154e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package com.sinata.download.service;
 
import android.app.IntentService;
import android.app.Notification;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.widget.RemoteViews;
 
import androidx.annotation.Nullable;
 
import com.sinata.download.DownloadLibrary;
import com.sinata.download.R;
import com.sinata.download.http.DownLoadManager;
import com.sinata.download.http.DownloadListener;
import com.sinata.download.utils.Utils;
import com.sinata.download.utils.notification.MessageNoticeManager;
 
import java.io.File;
 
/**
 * author:Created by zhaohaoting on 2019/1/30
 * email:526309416@qq.com
 * desc:下载视频
 */
public class DownloadService extends IntentService {
 
    private static final int NOTIFY_DOWNLOAD = 0;
    private static final int NOTIFY_FINISH = 1;
    private static final int NOTIFY_FAILURE = 2;
    //下载音乐
    public static final int TYPE_MUSIC = 0;
    //下载视频
    public static final int TYPE_VIDEO = 1;
    //下载图片
    public static final int TYPE_PICTURE = 2;
    //下载apk
    public static final int TYPE_APK = 3;
    //下载其它文件
    public static final int TYPE_FILE = 4;
 
    private int type;
 
    @Override
    public void onCreate() {
        super.onCreate();
    }
 
    private Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    createNotification(NOTIFY_DOWNLOAD, 0, null);
                    break;
                case 1:
                    createNotification(NOTIFY_DOWNLOAD, msg.arg1, null);
                    break;
                case 2:
                    createNotification(NOTIFY_FINISH, 0, msg.obj);
                    if (type == TYPE_APK) {
                        ApkDownLoadListener apkDownLoadListener = DownloadLibrary.getApkDownLoadListener();
                        if (apkDownLoadListener != null) {
                            apkDownLoadListener.downLoadSuccess((String) msg.obj);
                        }
                    }
                    break;
                case 3:
                    createNotification(NOTIFY_FAILURE, 0, null);
                    break;
            }
            return true;
        }
    });
 
    public DownloadService() {
        super("DownloadService");
    }
 
    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        if (intent != null) {
            String url = intent.getStringExtra("url");
            type = intent.getIntExtra("type", 4);
            String[] split = url.split("/");
            int length = split.length;
            if (length <= 0) return;
            String fileName = split[length - 1];
            switch (type) {
                case TYPE_MUSIC:
                    download(url, DownloadLibrary.MUSIC_PATH + fileName);
                    break;
                case TYPE_VIDEO:
                    download(url, DownloadLibrary.VIDEO_PATH + fileName);
                    break;
                case TYPE_PICTURE:
                    download(url, DownloadLibrary.PICTURE_PATH + fileName);
                    break;
                case TYPE_APK:
                    download(url, DownloadLibrary.APK_PATH + fileName);
                    break;
                case TYPE_FILE:
                    download(url, DownloadLibrary.FILE_PATH + fileName);
                    break;
            }
        }
    }
 
    /**
     * 下载
     *
     * @param url
     * @param filePath 文件保存全路径
     */
    private void download(String url, final String filePath) {
        DownLoadManager.downLoadFile(url, filePath, new DownloadListener() {
 
            int progress = 0;
 
            @Override
            public void onStartDownload() {
                Message message = Message.obtain(mHandler);
                message.what = 0;
                message.sendToTarget();
            }
 
            @Override
            public void onProgress(final int progress) {
                if (this.progress != progress) {
                    this.progress = progress;
                    Message message = Message.obtain(mHandler);
                    message.what = 1;
                    message.arg1 = progress;
                    message.sendToTarget();
                }
            }
 
            @Override
            public void onFinishDownload(boolean isHasLocation) {
                Message message = Message.obtain(mHandler);
                message.what = 2;
                message.obj = filePath;
                message.sendToTarget();
                if (type == TYPE_VIDEO) {
                    Utils.insertIntoMediaStore(DownloadService.this, true, new File(filePath), System.currentTimeMillis());
                } else if (type == TYPE_PICTURE) {
                    Utils.insertIntoMediaStore(DownloadService.this, false, new File(filePath), System.currentTimeMillis());
                } else if (type == TYPE_APK) {
 
//                    Utils.installApk(DownloadService.this, filePath);
                }
            }
 
            @Override
            public void onFail(String errorInfo) {
                Message message = Message.obtain(mHandler);
                message.what = 3;
                message.sendToTarget();
            }
        });
    }
 
    /**
     * 设置通知
     *
     * @param notifyId
     * @param progress
     */
    private void createNotification(int notifyId, int progress, Object obj) {
        switch (notifyId) {
            case NOTIFY_DOWNLOAD:
                RemoteViews remoteViews = new RemoteViews(DownloadLibrary.getContext().getApplicationInfo().packageName, R.layout.notify_custom_view_layout);
                remoteViews.setTextViewText(R.id.notify_tv, getString(R.string.downloading_));
                remoteViews.setProgressBar(R.id.notify_progress_pb, 100, progress, false);
                if (progress == 0) {
                    remoteViews.setTextViewText(R.id.notify_progress_tv, getString(R.string.loading_));
                } else {
                    remoteViews.setTextViewText(R.id.notify_progress_tv, progress + "%");
                }
                remoteViews.setImageViewResource(R.id.notify_download_iv, DownloadLibrary.getIc_launcher());
                Notification notice1 = MessageNoticeManager.getInstance().createNotice(this, remoteViews);
                MessageNoticeManager.getInstance().notify(NOTIFY_DOWNLOAD, notice1);
                break;
            case NOTIFY_FINISH:
                Notification notice = MessageNoticeManager.getInstance().createNotice(this, getString(R.string.download_done), getString(R.string.save_location_ed), getString(R.string.download_done), Utils.getEntranceIntent(this, (String) obj));
                MessageNoticeManager.getInstance().notify(NOTIFY_FINISH, notice);
                MessageNoticeManager.getInstance().cancel(NOTIFY_DOWNLOAD);
                break;
            case NOTIFY_FAILURE:
                Notification notice2 = MessageNoticeManager.getInstance().createNotice(this, getString(R.string.download_faliure), getString(R.string.download_tips_desc), getString(R.string.download_faliure));
                MessageNoticeManager.getInstance().notify(NOTIFY_FAILURE, notice2);
                MessageNoticeManager.getInstance().cancel(NOTIFY_DOWNLOAD);
                break;
            default:
                break;
        }
    }
 
 
}