|
|
@@ -14,8 +14,11 @@ import android.support.v4.app.NotificationCompat;
|
|
|
import android.text.TextUtils;
|
|
|
import android.view.LayoutInflater;
|
|
|
import android.view.ViewGroup;
|
|
|
+import android.widget.RemoteViews;
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
+import com.sheep.jiuyan.samllsheep.R;
|
|
|
+
|
|
|
import java.lang.reflect.Field;
|
|
|
import java.lang.reflect.Method;
|
|
|
import java.util.LinkedList;
|
|
|
@@ -27,8 +30,10 @@ import java.util.LinkedList;
|
|
|
public class NotificationsUtils {
|
|
|
private static final String CHECK_OP_NO_THROW = "checkOpNoThrow";
|
|
|
private static final String OP_POST_NOTIFICATION = "OP_POST_NOTIFICATION";
|
|
|
+
|
|
|
/**
|
|
|
* 8.0以上获取t通知栏状态
|
|
|
+ *
|
|
|
* @param context
|
|
|
* @return
|
|
|
*/
|
|
|
@@ -49,6 +54,7 @@ public class NotificationsUtils {
|
|
|
return true;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
public static boolean isNotificationEnabled(Context context) {
|
|
|
boolean Enabled;
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
@@ -58,7 +64,7 @@ public class NotificationsUtils {
|
|
|
return true;
|
|
|
}
|
|
|
}
|
|
|
- if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
|
|
AppOpsManager mAppOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
|
|
|
ApplicationInfo appInfo = context.getApplicationInfo();
|
|
|
String pkg = context.getApplicationContext().getPackageName();
|
|
|
@@ -78,7 +84,7 @@ public class NotificationsUtils {
|
|
|
e.printStackTrace();
|
|
|
Enabled = false;
|
|
|
}
|
|
|
- }else{
|
|
|
+ } else {
|
|
|
Enabled = true;
|
|
|
}
|
|
|
|
|
|
@@ -91,7 +97,7 @@ public class NotificationsUtils {
|
|
|
public static void goToNotificationSetting(Context context) {
|
|
|
Intent intent = new Intent();
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
- if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1 && TextUtils.equals(android.os.Build.BRAND, "360")){// 360 8.1.0 不能跳转成功,直接跳转应用详情设置界面
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1 && TextUtils.equals(android.os.Build.BRAND, "360")) {// 360 8.1.0 不能跳转成功,直接跳转应用详情设置界面
|
|
|
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
|
|
|
intent.setData(Uri.fromParts("package", context.getPackageName(), null));
|
|
|
} else {
|
|
|
@@ -112,6 +118,7 @@ public class NotificationsUtils {
|
|
|
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
context.startActivity(intent);
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
* 打开允许通知的设置页
|
|
|
*/
|
|
|
@@ -122,6 +129,7 @@ public class NotificationsUtils {
|
|
|
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
context.startActivity(intent);
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
* 打开允许通知的设置页
|
|
|
*/
|
|
|
@@ -133,6 +141,7 @@ public class NotificationsUtils {
|
|
|
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
context.startActivity(intent);
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
* 打开允许通知的设置页
|
|
|
*/
|
|
|
@@ -145,6 +154,7 @@ public class NotificationsUtils {
|
|
|
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
context.startActivity(intent);
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
* 打开允许通知的设置页
|
|
|
*/
|
|
|
@@ -162,42 +172,58 @@ public class NotificationsUtils {
|
|
|
}
|
|
|
|
|
|
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) {
|
|
|
+ RemoteViews view = buildFakeRemoteViews(context);
|
|
|
+ int layoutId = view.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 RemoteViews buildFakeRemoteViews(Context context) {
|
|
|
+ Notification.Builder builder;
|
|
|
+ builder = new Notification.Builder(context);
|
|
|
+ builder.setContentTitle("1")
|
|
|
+ .setContentText("1")
|
|
|
+ .setTicker("fackTicker");
|
|
|
+ RemoteViews remoteViews = null;
|
|
|
+ if (builder != null) {
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
|
|
+ remoteViews = builder.createContentView();
|
|
|
+ } else {
|
|
|
+ Notification notification = builder.getNotification();
|
|
|
+ remoteViews = notification.contentView;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return remoteViews;
|
|
|
+ }
|
|
|
+
|
|
|
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) {
|
|
|
+ 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<>();
|
|
|
+ int color = Color.TRANSPARENT;
|
|
|
+ LinkedList<ViewGroup> viewGroups = new LinkedList<>();
|
|
|
viewGroups.add(viewGroupSource);
|
|
|
- while (viewGroups.size()>0) {
|
|
|
- ViewGroup viewGroup1=viewGroups.getFirst();
|
|
|
+ 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();
|
|
|
+ } else if (viewGroup1.getChildAt(i) instanceof TextView) {
|
|
|
+ if (((TextView) viewGroup1.getChildAt(i)).getCurrentTextColor() != -1) {
|
|
|
+ color = ((TextView) viewGroup1.getChildAt(i)).getCurrentTextColor();
|
|
|
}
|
|
|
}
|
|
|
}
|