billyyoyo пре 6 година
родитељ
комит
a8467432e7

+ 57 - 0
app/src/main/java/com/sheep/gamegroup/view/activity/NotificationsUtils.java

@@ -1,17 +1,24 @@
 package com.sheep.gamegroup.view.activity;
 
 import android.app.AppOpsManager;
+import android.app.Notification;
 import android.app.NotificationManager;
 import android.content.Context;
 import android.content.Intent;
 import android.content.pm.ApplicationInfo;
+import android.graphics.Color;
 import android.net.Uri;
 import android.os.Build;
 import android.provider.Settings;
+import android.support.v4.app.NotificationCompat;
 import android.text.TextUtils;
+import android.view.LayoutInflater;
+import android.view.ViewGroup;
+import android.widget.TextView;
 
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
+import java.util.LinkedList;
 
 /**
  * Created by Administrator on 2017/8/25 0025.
@@ -149,4 +156,54 @@ public class NotificationsUtils {
         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         context.startActivity(intent);
     }
+
+    public static boolean isDarkNotificationTheme(Context context) {
+        return !isSimilarColor(Color.BLACK, getNotificationColor(context));
+    }
+
+    public static int getNotificationColor(Context context) {
+        NotificationCompat.Builder builder=new NotificationCompat.Builder(context);
+        Notification notification=builder.build();
+        int layoutId=notification.contentView.getLayoutId();
+        ViewGroup viewGroup= (ViewGroup) LayoutInflater.from(context).inflate(layoutId, null, false);
+        if (viewGroup.findViewById(android.R.id.title)!=null) {
+            return ((TextView) viewGroup.findViewById(android.R.id.title)).getCurrentTextColor();
+        }
+        return findColor(viewGroup);
+    }
+
+    private static boolean isSimilarColor(int baseColor, int color) {
+        int simpleBaseColor=baseColor|0xff000000;
+        int simpleColor=color|0xff000000;
+        int baseRed=Color.red(simpleBaseColor)-Color.red(simpleColor);
+        int baseGreen=Color.green(simpleBaseColor)-Color.green(simpleColor);
+        int baseBlue=Color.blue(simpleBaseColor)-Color.blue(simpleColor);
+        double value=Math.sqrt(baseRed*baseRed+baseGreen*baseGreen+baseBlue*baseBlue);
+        if (value<180.0) {
+            return true;
+        }
+        return false;
+    }
+
+    private static int findColor(ViewGroup viewGroupSource) {
+        int color= Color.TRANSPARENT;
+        LinkedList<ViewGroup> viewGroups=new LinkedList<>();
+        viewGroups.add(viewGroupSource);
+        while (viewGroups.size()>0) {
+            ViewGroup viewGroup1=viewGroups.getFirst();
+            for (int i = 0; i < viewGroup1.getChildCount(); i++) {
+                if (viewGroup1.getChildAt(i) instanceof ViewGroup) {
+                    viewGroups.add((ViewGroup) viewGroup1.getChildAt(i));
+                }
+                else if (viewGroup1.getChildAt(i) instanceof TextView) {
+                    if (((TextView) viewGroup1.getChildAt(i)).getCurrentTextColor()!=-1) {
+                        color=((TextView) viewGroup1.getChildAt(i)).getCurrentTextColor();
+                    }
+                }
+            }
+            viewGroups.remove(viewGroup1);
+        }
+        return color;
+    }
+
 }

+ 8 - 1
app/src/main/java/com/sheep/jiuyan/samllsheep/service/DownloadService.java

@@ -45,6 +45,7 @@ import com.sheep.gamegroup.util.FileUtil;
 import com.sheep.gamegroup.util.LogUtil;
 import com.sheep.gamegroup.util.StringUtils;
 import com.sheep.gamegroup.util.TestUtil;
+import com.sheep.gamegroup.view.activity.NotificationsUtils;
 import com.sheep.jiuyan.samllsheep.R;
 import com.sheep.jiuyan.samllsheep.SheepApp;
 import com.sheep.jiuyan.samllsheep.utils.G;
@@ -411,7 +412,11 @@ public class DownloadService extends Service {
                 INCREMENT.set(1);
                 id = 1;
             }
-            view = new RemoteViews(SheepApp.getInstance().getPackageName(), R.layout.download_notification);
+            if (NotificationsUtils.isDarkNotificationTheme(SheepApp.getInstance())) {
+                view = new RemoteViews(SheepApp.getInstance().getPackageName(), R.layout.download_notification_dark);
+            } else {
+                view = new RemoteViews(SheepApp.getInstance().getPackageName(), R.layout.download_notification_light);
+            }
             view.setTextViewText(R.id.title_view, SheepApp.getInstance().getString(R.string.app_name) + "下载中......");
             Intent din = new Intent(SheepApp.getInstance(), DownloadService.class).putExtra("download_id", id).putExtra("download_url", task.getUrl());
             PendingIntent delIntent = PendingIntent.getService(SheepApp.getInstance(), 0, din, PendingIntent.FLAG_UPDATE_CURRENT);
@@ -477,6 +482,8 @@ public class DownloadService extends Service {
             Notification notification;
             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                 NotificationChannel mChannel = new NotificationChannel(channelId, name, NotificationManager.IMPORTANCE_HIGH);
+                mChannel.enableVibration(false);
+                mChannel.enableLights(false);
                 notificationManager.createNotificationChannel(mChannel);
                 notification = new NotificationCompat.Builder(SheepApp.getInstance(), channelId)
                         .setCustomContentView(view)

app/src/main/res/layout/download_notification.xml → app/src/main/res/layout/download_notification_dark.xml


+ 48 - 0
app/src/main/res/layout/download_notification_light.xml

@@ -0,0 +1,48 @@
+<?xml version="1.0" encoding="utf-8"?>
+<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="60dp"
+    android:id="@+id/body_view"
+    android:paddingLeft="16dp"
+    android:paddingRight="16dp">
+
+    <ImageView
+        android:id="@+id/icon_view"
+        android:src="@mipmap/icon_unread"
+        android:layout_centerVertical="true"
+        android:layout_width="@dimen/dp_40"
+        android:layout_height="@dimen/dp_40" />
+
+    <TextView
+        android:id="@+id/title_view"
+        android:layout_toRightOf="@+id/icon_view"
+        android:layout_marginLeft="12dp"
+        android:textSize="@dimen/sp_14"
+        android:layout_marginTop="12dp"
+        android:textColor="@color/black_333333"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content" />
+
+    <ProgressBar
+        android:id="@+id/progress_view"
+        style="?android:attr/progressBarStyleHorizontal"
+        android:max="100"
+        android:progress="0"
+        android:layout_margin="10dp"
+        android:layout_alignParentBottom="true"
+        android:layout_toRightOf="@+id/icon_view"
+        android:layout_toLeftOf="@+id/delete_btn"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content" />
+
+    <ImageView
+        android:id="@+id/delete_btn"
+        android:src="@mipmap/cross_delete"
+        android:padding="10dp"
+        android:layout_alignParentRight="true"
+        android:layout_centerVertical="true"
+        android:layout_width="@dimen/view_size_30"
+        android:layout_height="@dimen/view_size_30"
+        android:tint="@color/black_333333"/>
+
+</RelativeLayout>