package com.hainaos.vc.service; import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; import android.content.Intent; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Build; import android.os.IBinder; import android.text.TextUtils; import android.util.Log; import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationManagerCompat; import androidx.core.content.FileProvider; import com.arialyy.annotations.Download; import com.arialyy.aria.core.Aria; import com.arialyy.aria.core.task.DownloadTask; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import com.hainaos.vc.R; import com.hainaos.vc.bean.CategoryVideoInfo; import com.hjq.toast.Toaster; import java.io.File; import java.lang.reflect.Type; public class DownloadService extends Service { private static final String TAG = "DownloadService"; public DownloadService() { } @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); Aria.download(this).register(); mNotificationManagerCompat = NotificationManagerCompat.from(this); // createNotificationChannel(); createDownloadNotificationChannel(); // sendSimpleNotification(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.e(TAG, "onStartCommand: " + intent); return START_STICKY; } @Override public void onDestroy() { super.onDestroy(); } private static final String CHANNEL_ID = "CHANNEL_ID"; private static final String CHANNEL_NAME = "系统通知"; private static final String CHANNEL_DESCRIPTION = "海纳通知"; private static final String CHANNEL_DOWNLOAD_ID = "DOWNLOAD_CHANNEL"; private static final String CHANNEL_DOWNLOAD_NAME = "下载管理"; private static final String CHANNEL_DOWNLOAD_DESCRIPTION = "下载管理通知"; private void createNotificationChannel() { // Create the NotificationChannel, but only on API 26+ because // the NotificationChannel class is new and not in the support library if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT); channel.setDescription(CHANNEL_DESCRIPTION); // Register the channel with the system; you can't change the importance // or other notification behaviors after this NotificationManager notificationManager = getSystemService(NotificationManager.class); notificationManager.createNotificationChannel(channel); } } private NotificationManagerCompat mNotificationManagerCompat; private int NotificationID = 7890; private void sendSimpleNotification() { Intent intent = new Intent(this, DownloadService.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle("海纳正在运行") // .setContentText("测试内容") .setAutoCancel(false) .setShowWhen(false) .setContentIntent(pendingIntent) .setOngoing(true) .setOnlyAlertOnce(true) .setPriority(NotificationCompat.PRIORITY_MAX); // notificationId is a unique int for each notification that you must define // mNotificationManagerCompat.notify(NotificationID, builder.build()); startForeground(NotificationID, builder.build()); } private void createDownloadNotificationChannel() { // Create the NotificationChannel, but only on API 26+ because // the NotificationChannel class is new and not in the support library if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel(CHANNEL_DOWNLOAD_ID, CHANNEL_DOWNLOAD_NAME, NotificationManager.IMPORTANCE_DEFAULT); channel.setDescription(CHANNEL_DOWNLOAD_DESCRIPTION); // Register the channel with the system; you can't change the importance // or other notification behaviors after this NotificationManager notificationManager = getSystemService(NotificationManager.class); notificationManager.createNotificationChannel(channel); } } private void sendDownloadRunning(CategoryVideoInfo ariaDownloadInfo, int progress) { NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_DOWNLOAD_ID) .setSmallIcon(R.mipmap.ic_launcher) .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)) .setContentTitle(ariaDownloadInfo.getName()) .setContentText("下载中:" + progress + "%") .setAutoCancel(true) .setShowWhen(true) .setOngoing(false) .setOnlyAlertOnce(true) .setProgress(100, progress, false) .setPriority(NotificationCompat.PRIORITY_HIGH); // notificationId is a unique int for each notification that you must define // mNotificationManagerCompat.notify(ariaDownloadInfo.getAppId(), builder.build()); startForeground(ariaDownloadInfo.getFile_size(), builder.build()); } private void sendDownloadComplete(CategoryVideoInfo ariaDownloadInfo, String path) { NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_DOWNLOAD_ID) .setSmallIcon(R.mipmap.ic_launcher) .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)) .setContentTitle(ariaDownloadInfo.getName()) .setContentText("下载完成") .setAutoCancel(true) .setShowWhen(true) .setOngoing(false) .setOnlyAlertOnce(true) .setPriority(NotificationCompat.PRIORITY_HIGH) // .setContentIntent(createIntent(path)) ; // notificationId is a unique int for each notification that you must define // mNotificationManagerCompat.notify(ariaDownloadInfo.getAppId(), builder.build()); startForeground(ariaDownloadInfo.getFile_size(), builder.build()); } /** * 设置通知点击事件 * * @return 点击事件 */ private PendingIntent createIntent(String path) { File file = new File(path); Intent intent = new Intent(Intent.ACTION_VIEW); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); //注意第二个参数,要保持和manifest中android:authorities的值相同 Uri uri = FileProvider.getUriForFile(DownloadService.this, getPackageName() + ".FileProvider", file); intent.setDataAndType(uri, "application/vnd.android.package-archive"); } else { intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive"); } PendingIntent pendingIntent = PendingIntent.getActivity(DownloadService.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); return pendingIntent; } private void sendDownloadFail(CategoryVideoInfo ariaDownloadInfo) { NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_DOWNLOAD_ID) .setSmallIcon(R.mipmap.ic_launcher) .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)) .setContentTitle(ariaDownloadInfo.getName()) .setContentText("下载失败") .setAutoCancel(true) .setShowWhen(true) .setOngoing(false) .setOnlyAlertOnce(true) .setPriority(NotificationCompat.PRIORITY_HIGH); // notificationId is a unique int for each notification that you must define // mNotificationManagerCompat.notify(ariaDownloadInfo.getAppId(), builder.build()); startForeground(ariaDownloadInfo.getFile_size(), builder.build()); } @Download.onTaskRunning void running(DownloadTask task) { String jsonString = task.getExtendField(); Log.e(TAG, "running: " + "正在下载:" + task.getPercent() + "% " + jsonString); CategoryVideoInfo ariaDownloadInfo = getAriaDownloadInfo(jsonString); if (ariaDownloadInfo != null) { Toaster.show("正在下载: " + ariaDownloadInfo.getName() + "\t" + task.getPercent() + "%"); sendDownloadRunning(ariaDownloadInfo, task.getPercent()); } } @Download.onTaskComplete void taskComplete(DownloadTask task) { String path = task.getFilePath(); Log.e(TAG, "taskComplete: " + path); if (path.endsWith(".hnv")) { // LenovoCsdkUtil.getInstance().installPackage(task.getFilePath()); String jsonString = task.getExtendField(); Log.e(TAG, "taskComplete: " + "下载完成:" + jsonString); CategoryVideoInfo ariaDownloadInfo = getAriaDownloadInfo(jsonString); if (ariaDownloadInfo != null) { Toaster.show("下载完成: " + "\t" + ariaDownloadInfo.getName()); sendDownloadComplete(ariaDownloadInfo, task.getFilePath()); } } } @Download.onTaskFail void taskFail(DownloadTask task, Exception e) { Log.e(TAG, "taskFail: "); String jsonString = task.getExtendField(); Log.e(TAG, "taskFail: " + "下载失败:" + jsonString); CategoryVideoInfo ariaDownloadInfo = getAriaDownloadInfo(jsonString); if (ariaDownloadInfo != null) { Toaster.show("下载失败: " + "\t" + ariaDownloadInfo.getName()); sendDownloadFail(ariaDownloadInfo); } } private CategoryVideoInfo getAriaDownloadInfo(String jsonString) { if (!TextUtils.isEmpty(jsonString)) { Gson gson = new Gson(); Type type = new TypeToken() { }.getType(); CategoryVideoInfo ariaDownloadInfo = null; try { ariaDownloadInfo = gson.fromJson(jsonString, type); } catch (Exception e) { Log.e(TAG, "getAriaDownloadInfo: " + e.getMessage()); } return ariaDownloadInfo; } else { return null; } } }