admin
2020-08-12 cefe2a41db4a275fb1e940a902cb156f1ed68d80
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
 * Copyright © 2012-2013 LiuZhongnan. All rights reserved.
 * 
 * Email:qq81595157@126.com
 * 
 * PROPRIETARY/CONFIDENTIAL.
 */
 
package com.youku.service.download;
 
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager.WifiLock;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.widget.RemoteViews;
 
import com.baseproject.utils.Logger;
import com.youku.player.YoukuPlayerConfiguration;
import com.youku.player.ui.R;
 
/**
 * DownloadListenerImpl.下载状态改变的监听实现
 * 
 * @author 刘仲男 qq81595157@126.com
 * @version v3.5
 * @created time 2012-11-5 下午1:16:02
 */
@SuppressLint("InlinedApi")
public class DownloadListenerImpl implements DownloadListener {
    private static final String TAG = "Download_ListenerImpl";
    // private static final long UPDATE_RATE = 3000;// 设置进度、通知、数据库更新时间间隔
    private static final String pageName = "缓存模块";
    public static Context context;
    public DownloadServiceManager download;
 
    public static NotificationManager nm;
    public static WifiLock wifiLock;
    public static WakeLock wakeLock;
    private DownloadInfo info;
 
    @SuppressWarnings("static-access")
    public DownloadListenerImpl(Context context, DownloadInfo info) {
        this.context = context;
        this.info = info;
        download = DownloadServiceManager.getInstance();
        if (nm == null)
            nm = (NotificationManager) context
                    .getSystemService(Context.NOTIFICATION_SERVICE);
        if (wifiLock == null)
            wifiLock = ((WifiManager) context
                    .getSystemService(Context.WIFI_SERVICE)).createWifiLock(
                    WifiManager.WIFI_MODE_FULL_HIGH_PERF,
                    context.getPackageName());
        if (wakeLock == null) {
            PowerManager pm = (PowerManager) context
                    .getSystemService(Context.POWER_SERVICE);
            wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                    "mywakelock");
        }
 
    }
 
    @Override
    public void onStart() {
            Logger.d(TAG, "onstart() :" + info.title);
        notify(info, "开始缓存" + info.title,
                "缓存中... - " + DownloadUtils.getProgress(info) + "%", false,
                true);
        info.startTime = System.currentTimeMillis();
        DownloadUtils.makeDownloadInfoFile(info);
        if (wakeLock != null)
            wakeLock.acquire();
        if (wifiLock != null)
            wifiLock.acquire();
        try {
            if (download.getCallback() != null)
                download.getCallback().onChanged(info);
        } catch (Exception e) {
            Logger.e(TAG, e);
        }
    }
 
    @Override
    public void onPause() {
            Logger.d(TAG, "onPause() :" + info.title);
        if (info.thread != null)
            info.thread.cancel();
        DownloadUtils.makeDownloadInfoFile(info);
        try {
            if (download.getCallback() != null)
                download.getCallback().onChanged(info);
        } catch (Exception e) {
            Logger.e(TAG, e);
        }
        if (!download.hasDownloadingTask()) {
            notify(info, info.title + "已暂停", "暂停中", true, false);
            download.startNewTask();
        }
 
        release();
    }
 
    @Override
    public void onCancel() {
            Logger.d(TAG, "onCancel() :" + info.title);
        DownloadUtils.makeDownloadInfoFile(info);
        // try {
        // if (download.getCallback() != null)
        // download.getCallback().onChanged(info);
        // } catch (Exception e) {
        // Logger.e(TAG, e);
        // }
    }
 
    @Override
    public void onException() {
            Logger.d(TAG, "onException() :" + info.title);
        if (info.thread != null) {
            info.thread.cancel();
            info.thread = null;
        }
        if (!download.hasDownloadingTask())
            notify(info, "等待缓存" + info.title, "等待中...", true, false);
        if (info.getExceptionId() == DownloadInfo.EXCEPTION_NO_SDCARD) {
            nm.cancel(IDownload.NOTIFY_ID);
        } else {
            DownloadUtils.makeDownloadInfoFile(info);
        }
        try {
            if (download.getCallback() != null)
                download.getCallback().onChanged(info);
        } catch (Exception e) {
            Logger.e(TAG, e);
        }
        release();
        if (info.getExceptionId() != DownloadInfo.EXCEPTION_NO_SPACE
                && info.getExceptionId() != DownloadInfo.EXCEPTION_NO_SDCARD)
            download.startNewTask();
    }
 
    @Override
    public void onFinish() {
            Logger.d(TAG, "onFinish() :" + info.title);
        notify(info, info.title + "缓存完成", "缓存完成", true, false);
        info.finishTime = System.currentTimeMillis();
        DownloadUtils.makeDownloadInfoFile(info);
        context.sendBroadcast(new Intent(BaseDownload.ACTION_DOWNLOAD_FINISH));
        try {
            if (download.getCallback() != null)
                download.getCallback().onFinish(info);
        } catch (Exception e) {
            Logger.e(TAG, e);
        }
        download.cleanRetry();
        release();
        download.startNewTask();
    }
 
    @Override
    public void onProgressChange(double progress) {
        long time = System.currentTimeMillis();
            Logger.d(TAG, "onProgressChange() :" + progress + "%");
        if (info.getState() == DownloadInfo.STATE_DOWNLOADING)
            notify(info, "开始缓存" + info.title,
                    "缓存中... - " + DownloadUtils.getProgress(info) + "%", false,
                    true);
        info.lastUpdateTime = time;
        DownloadUtils.makeDownloadInfoFile(info);
        try {
            if (download.getCallback() != null)
                download.getCallback().onChanged(info);
        } catch (Exception e) {
            Logger.e(TAG, e);
        }
    }
 
    @Override
    public void onWaiting() {
            Logger.d(TAG, "onwainting() :" + info.title);
        if (info.thread != null) {
            info.thread.cancel();
            info.thread = null;
        }
        if (!download.hasDownloadingTask())
            notify(info, "等待缓存" + info.title, "等待中...", true, false);
        DownloadUtils.makeDownloadInfoFile(info);
        try {
            if (download.getCallback() != null)
                download.getCallback().onChanged(info);
        } catch (Exception e) {
            Logger.e(TAG, e);
        }
    }
 
    /**
     * 更新通知
     * 
     * @param info
     * @param ticker
     * @param notify_state
     * @param playSound
     * @param iconRunning
     */
    private static void notify(DownloadInfo info, String ticker,
            String contentText, boolean autoCancel, boolean iconRunning) {
        if (info.notification == null) {
            info.notification = createNotification(context);
        }
        Logger.d("DownloadFlow","createNotification");
        Notification n = info.notification;
        n.icon = iconRunning ? android.R.drawable.stat_sys_download
                : android.R.drawable.stat_sys_download_done;
        n.flags = autoCancel ? Notification.FLAG_AUTO_CANCEL
                : Notification.FLAG_NO_CLEAR;
//        n.flags |= Notification.FLAG_ONGOING_EVENT;
        n.tickerText = ticker;
        String pkg = YoukuPlayerConfiguration.context.getPackageName();
        int icon_id = YoukuPlayerConfiguration.context.getResources().getIdentifier("noitfy_icon", "id", pkg);
        int text_id = YoukuPlayerConfiguration.context.getResources().getIdentifier("notify_text", "id", pkg);
        int state_id = YoukuPlayerConfiguration.context.getResources().getIdentifier("notify_state", "id", pkg);
        int notify_processbar_id = YoukuPlayerConfiguration.context.getResources().getIdentifier("notify_processbar", "id", pkg);
        
        n.contentView.setImageViewResource(icon_id, n.icon);
        n.contentView.setTextViewText(text_id, info.title);
        n.contentView.setTextViewText(state_id, contentText);
        n.contentView.setProgressBar(
                notify_processbar_id,
                100,
                (int) info.getProgress(),
                info.getState() == DownloadInfo.STATE_WAITING
                        || info.getState() == DownloadInfo.STATE_PAUSE
                        || info.getState() == DownloadInfo.STATE_EXCEPTION);
        if (info.getState() == DownloadInfo.STATE_FINISH) {
            n.defaults = Notification.DEFAULT_SOUND;
            n.contentView.setProgressBar(notify_processbar_id, 100, 100,
                    false);
            Intent i = new Intent(context, YoukuPlayerConfiguration.instance.getCachedActivityClass());
            i.putExtra("go", "downloaded");
            n.contentIntent = PendingIntent.getActivity(context, 4, i,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            needChange = true;
        } else {
            n.defaults = 0;
            if (needChange) {
                Intent i = new Intent(context, YoukuPlayerConfiguration.instance.getCachingActivityClass());
                i.putExtra("go", "downloading");
                n.contentIntent = PendingIntent.getActivity(context, 4, i,
                        PendingIntent.FLAG_UPDATE_CURRENT);
                needChange = false;
            }
        }
        YoukuPlayerConfiguration.savePreference(IDownload.KEY_LAST_NOTIFY_TASKID, info.taskId);
        nm.notify(IDownload.NOTIFY_ID, n);
    }
 
    private static boolean needChange = true;
 
    /**
     * TODO 创建通知对象
     */
    private static Notification createNotification(Context c) {
        Notification n = new Notification();
        Class<? extends Activity> cacheActivity = YoukuPlayerConfiguration.instance.getCachingActivityClass();
        Intent i = new Intent(c, cacheActivity);
        i.putExtra("go", "downloading");
        n.contentIntent = PendingIntent.getActivity(c, 4, i,
                PendingIntent.FLAG_UPDATE_CURRENT);
        n.contentView = new RemoteViews(c.getPackageName(), R.layout.notify);
        return n;
    }
 
    private void release() {
        if (wifiLock != null && wifiLock.isHeld())
            wifiLock.release();
        if (wakeLock != null && wakeLock.isHeld())
            wakeLock.release();
    }
}