This commit is contained in:
18142669586
2024-02-23 16:07:50 +08:00
commit a02f7b2e22
5737 changed files with 438436 additions and 0 deletions

View File

@@ -0,0 +1,330 @@
package com.shayu.phonelive.utils;
import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.net.Uri;
import android.os.Build;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RemoteViews;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.request.transition.Transition;
import com.makeramen.roundedimageview.RoundedImageView;
import com.shayu.phonelive.AppContext;
import com.shayu.phonelive.activity.LauncherActivity;
import com.yunbao.common.bean.NotificationMsgBean;
import com.yunbao.common.glide.ImgLoader;
import com.yunbao.common.utils.DialogUitl;
import com.yunbao.common.utils.SpUtil;
import com.yunbao.common.utils.WordUtil;
import com.yunbao.main.activity.MainActivity;
import org.json.JSONException;
import org.json.JSONObject;
import io.rong.imlib.ConnectChangeReceiver;
import io.rong.push.PushType;
import io.rong.push.notification.PushMessageReceiver;
import io.rong.push.notification.PushNotificationMessage;
import myname.pdlive.shayu.R;
public class CustomMessageReceiver extends PushMessageReceiver {
private String createNotificationChannel(Context context, String channelID, String channelNAME, int level) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationManager manager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
NotificationChannel channel = new NotificationChannel(channelID, channelNAME, level);
channel.setSound(Uri.parse("android.resource://" + context.getPackageName() + "/raw/pdlive_sound"), null);
manager.createNotificationChannel(channel);
return channelID;
} else {
return null;
}
}
@Override
public boolean onNotificationMessageClicked(
Context context, PushType pushType, PushNotificationMessage message) {
if (pushType.equals(PushType.GOOGLE_FCM)) {
Log.i("gmc", "gmc");
// TODO
} else {
Log.i("gmc", "gmc1111");
}
// 返回 true 表示拦截false 为不拦截
return true;
}
NotificationMsgBean msg = new NotificationMsgBean();
@Override
public boolean onNotificationMessageArrived(Context context, PushType pushType, PushNotificationMessage notificationMessage) {
Log.i("gmc", notificationMessage.getPushContent() + "VVV" + notificationMessage.getPushTitle() + "gmc11112222" + notificationMessage.getExtra());
if (!SpUtil.getInstance().getBooleanValue("NOTIFICATION")) {
if (AppContext.activityWeakReference != null) {
Activity activity = AppContext.activityWeakReference.get();
if (activity != null) {
DialogUitl.showSimpleDialog(activity, WordUtil.isNewZh()?"应用需要通知权限":"Application requires notification permission", new DialogUitl.SimpleCallback() {
@Override
public void onConfirmClick(Dialog dialog, String content) {
Intent intent = new Intent();
intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
intent.putExtra("app_package", context.getPackageName());
intent.putExtra("app_uid", context.getApplicationInfo().uid);
// for Android 8 and above
intent.putExtra("android.provider.extra.APP_PACKAGE", context.getPackageName());
activity.startActivity(intent);
}
});
}
}
}
if (notificationMessage.getExtra() == null) {
msg.setImg("" + notificationMessage.getSenderPortrait());
msg.setTitle(notificationMessage.getPushTitle());
msg.setContent(notificationMessage.getPushContent());
msg.setType("-1");
sendNotification(context, msg);
} else {
try {
JSONObject obj = new JSONObject(notificationMessage.getExtra());
Log.i("gmc", "1111222");
//开播
if (obj.getString("type").equals("1") || obj.getString("type").equals("9")) {
Log.i("gmc", "1111");
msg.setImg("" + obj.getString("avatar"));
msg.setTitle(notificationMessage.getPushTitle());
msg.setContent(notificationMessage.getPushContent());
msg.setLiveuid(obj.getString("liveuid"));
msg.setType("1");
liveNotification(context, msg);
//发布动态 2
//点赞 3
//评论 4
//鼓励 5
} else if (obj.getString("type").equals("2") || obj.getString("type").equals("3") || obj.getString("type").equals("4") || obj.getString("type").equals("5")) {
msg.setImg("" + obj.getString("avatar"));
msg.setTitle(notificationMessage.getPushTitle());
msg.setContent(notificationMessage.getPushContent());
msg.setType("2");
dynamicNotification(context, msg);
} else if (obj.getString("type").equals("8") || obj.getString("type").equals("7")) {
msg.setImg("" + obj.getString("avatar"));
msg.setTitle(notificationMessage.getPushTitle());
msg.setContent(notificationMessage.getPushContent());
msg.setType(obj.getString("type"));
msg.setActivityurl(obj.getString("activityUrl"));
serviceNotification(context, msg);
}
} catch (JSONException e) {
Log.i("gmc", "1111" + e.getMessage());
e.printStackTrace();
}
}
return true;
}
public static Bitmap makeRoundCorner(Bitmap bitmap, float roundPx) {
try {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;// 颜色值0xff---alpha
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);// Rect是使用int类型作为数值RectF是使用float类型作为数值
// --------抗锯齿-------//
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
final Rect src = new Rect(0, 0, bitmap.getWidth(),
bitmap.getHeight());
canvas.drawBitmap(bitmap, null, rect, paint);
return output;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
//私聊
private void sendNotification(Context context, NotificationMsgBean message) {
// 设置通知的点击行为:这里启动一个 Activity
Intent intent = new Intent(context, MainActivity.class);
intent.setFlags(FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE);
} else {
pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
}
String channelId = createNotificationChannel(context, "my_channel_ID", "my_channel_NAME", NotificationManager.IMPORTANCE_HIGH);
// 构建 remoteView
RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.notification_message);
remoteView.setTextViewText(R.id.title, message.getTitle());
remoteView.setTextViewText(R.id.content, message.getContent());
Glide.with(context).asBitmap().load(message.getImg()).into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
remoteView.setImageViewBitmap(R.id.img, makeRoundCorner(resource, 50));
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelId);
// 设置自定义 RemoteViews
builder.setContent(remoteView).setSmallIcon(R.drawable.small_icons);
// 设置通知的优先级(悬浮通知)
builder.setPriority(NotificationCompat.PRIORITY_MAX);
builder.setContentIntent(pendingIntent);
builder.setAutoCancel(true);
Notification notification = builder.build();
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, notification);
}
});
}
int id = 1;
//动态
private void dynamicNotification(Context context, NotificationMsgBean message) {
// 设置通知的点击行为:这里启动一个 Activity
Intent intent = new Intent(context, MainActivity.class);
intent.putExtra("type", "2");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE);
} else {
pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
String channelId = createNotificationChannel(context, "my_channel_ID", "my_channel_NAME", NotificationManager.IMPORTANCE_HIGH);
// 构建 remoteView
RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.notification_message);
remoteView.setTextViewText(R.id.title, message.getTitle());
remoteView.setTextViewText(R.id.content, message.getContent());
remoteView.setImageViewResource(R.id.img, R.mipmap.ic_launcher);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelId);
// 设置自定义 RemoteViews
builder.setContent(remoteView).setSmallIcon(R.drawable.small_icons);
// 设置通知的优先级(悬浮通知)
builder.setPriority(NotificationCompat.PRIORITY_MAX);
builder.setContentIntent(pendingIntent);
builder.setAutoCancel(true);
Notification notification = builder.build();
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
id = (int) (id + System.currentTimeMillis());
manager.notify(id, notification);
}
//开播
private void liveNotification(Context context, NotificationMsgBean message) {
// 设置通知的点击行为:这里启动一个 Activity
Intent intent = new Intent(context, LauncherActivity.class);
intent.setFlags(FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("liveid", message.getLiveuid());
Log.e("gmc", message.getLiveuid() + "21121");
PendingIntent pendingIntent;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE);
} else {
pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
String channelId = createNotificationChannel(context, "my_channel_ID", "my_channel_NAME", NotificationManager.IMPORTANCE_HIGH);
// 构建 remoteView
RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.notification_message);
remoteView.setTextViewText(R.id.title, message.getTitle());
remoteView.setTextViewText(R.id.content, message.getContent());
Glide.with(context).asBitmap().load(message.getImg()).into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
remoteView.setImageViewBitmap(R.id.img, makeRoundCorner(resource, 50));
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelId);
// 设置自定义 RemoteViews
builder.setContent(remoteView).setSmallIcon(R.drawable.small_icons);
// 设置通知的优先级(悬浮通知)
builder.setPriority(NotificationCompat.PRIORITY_MAX);
builder.setContentIntent(pendingIntent);
builder.setAutoCancel(true);
Notification notification = builder.build();
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
id = (int) (id + System.currentTimeMillis());
manager.notify(id, notification);
}
});
Log.i("gmc", "11123231");
}
//后台
private void serviceNotification(Context context, NotificationMsgBean message) {
// 设置通知的点击行为:这里启动一个 Activity
Intent intent = new Intent(context, MainActivity.class);
intent.putExtra("type", message.getType());
if (message.getType().equals("8")) {
intent.putExtra("activityUrl", message.getActivityurl());
}
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE);
} else {
pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
String channelId = createNotificationChannel(context, "my_channel_ID", "my_channel_NAME", NotificationManager.IMPORTANCE_HIGH);
// 构建 remoteView
RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.notification_message);
remoteView.setTextViewText(R.id.title, message.getTitle());
remoteView.setTextViewText(R.id.content, message.getContent());
remoteView.setImageViewResource(R.id.img, R.mipmap.ic_launcher);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelId);
// 设置自定义 RemoteViews
builder.setContent(remoteView).setSmallIcon(R.drawable.small_icons);
// 设置通知的优先级(悬浮通知)
builder.setPriority(NotificationCompat.PRIORITY_MAX);
builder.setContentIntent(pendingIntent);
builder.setAutoCancel(true);
Notification notification = builder.build();
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
id = (int) (id + System.currentTimeMillis());
manager.notify(id, notification);
}
}

View File

@@ -0,0 +1,90 @@
package com.shayu.phonelive.utils;
import static java.text.DateFormat.DEFAULT;
import android.content.Context;
import com.yunbao.common.CommonAppConfig;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.Objects;
public class LogUtils {
/**
* 采集所有日志
*/
public static void start(Context context) {
new Thread(new Runnable() {
@Override
public void run() {
try {
String[] exec = new String[]{"logcat", "-c"};
Runtime.getRuntime().exec(exec).waitFor();
exec = new String[]{"logcat", "-v", "color", "UTC-8"};
Process process = Runtime.getRuntime().exec(exec);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
PrintWriter writer = null;
String title = "[PDLIVE]" + CommonAppConfig.getInstance().getUid() + "_";
String today = title + SimpleDateFormat.getDateInstance(DEFAULT, Locale.CHINA).format(new Date());
File dir = new File(context.getDir("files", Context.MODE_PRIVATE).getAbsolutePath() + File.separator);
if (dir.listFiles() != null) {
for (File file : dir.listFiles()) {
if (file.getName().endsWith(".log") && !"error.log".equals(file.getName())) {
String fileName = file.getName().replace(".log", "").split("_")[1];
if (isDelLog(fileName)) {
file.delete();
}
}
}
}
File saveFile = new File(context.getDir("files", Context.MODE_PRIVATE).getAbsolutePath() + File.separator + today + ".log");
FileOutputStream os = new FileOutputStream(saveFile, true);
writer = new PrintWriter(os);
while ((line = bufferedReader.readLine()) != null) {
writer.append(line).write("\n");
writer.flush();
}
writer.flush();
writer.close();
exec = new String[]{"logcat", "-c"};
Runtime.getRuntime().exec(exec).waitFor();
bufferedReader.close();
start(context);
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
/**
* 判断是否是要删除的日志
**/
private static boolean isDelLog(String time) {
try {
Calendar timeCal = Calendar.getInstance(Locale.CHINA);
timeCal.setTime(Objects.requireNonNull(SimpleDateFormat.getDateInstance(DEFAULT, Locale.CHINA).parse(time)));
Calendar calendar = Calendar.getInstance(Locale.CHINA);
calendar.setTime(new Date());
calendar.add(Calendar.DATE, -3);
return calendar.after(timeCal);
} catch (ParseException e) {
e.printStackTrace();
}
return false;
}
}

View File

@@ -0,0 +1,112 @@
/*
package com.shayu.phonelive.utils;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.RingtoneManager;
import android.net.Uri;
import android.util.Log;
import android.widget.RemoteViews;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import com.google.android.gms.common.internal.zzm;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import com.yunbao.common.utils.ToastUtil;
import com.yunbao.main.activity.MainActivity;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Map;
import myname.pdlive.shayu.R;
public class MyNotificationService extends FirebaseMessagingService {
private String createNotificationChannel(String channelID, String channelNAME, int level) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationChannel channel = new NotificationChannel(channelID, channelNAME, level);
manager.createNotificationChannel(channel);
return channelID;
} else {
return null;
}
}
public void onMessageReceived(RemoteMessage message) {
// do nothing
Map<String, String> params = message.getData();
JSONObject object = new JSONObject(params);
Log.e("JSON_OBJECT", object.toString());
JSONObject remoteMessage = null;
try {
remoteMessage = new JSONObject(object.getString("message"));
Log.e("gmc","remoteMessage1"+remoteMessage.toString());
Log.e("gmc","remoteMessage"+remoteMessage.getString("fromUserName"));
} catch (JSONException e) {
e.printStackTrace();
}
// if(remoteMessage!=null && remoteMessage.isNull("fcm_token")) {
// String title = remoteMessage.getData().get("title");
// String message = remoteMessage.getData().get("text");
// String username = remoteMessage.getData().get("username");
// String uid = remoteMessage.getData().get("uid");
// String fcmToken = remoteMessage.getData().get("fcm_token");
// Log.e("gmc","remoteMessage"+remoteMessage.getData().get("username"));
//
// //Muestro la notifiación
//// sendNotification(title, message, username, uid, fcmToken);
// }else {
// /// Si es de tipo inserción la muestro sino no.
// //Es una nueva notificación de que alguien ha creado algo
// if(remoteMessage.getData().get("accion")!=null &&
// remoteMessage.getData().get("accion").compareTo("insert")==0)
//// displayNotification(remoteMessage.getNotification(), remoteMessage.getData());
//// //Envío los datos al RecyclerView correspondiente para que se actualice
//// addNotificacion(remoteMessage);
// Log.e("gmc","remoteMessage"+remoteMessage.getData().get("username"));
//
// }
// sendNotification("remoteMessage");
}
// private void sendNotification(String messageBody) {
//
// // 设置通知的点击行为:这里启动一个 Activity
// Intent intent = new Intent(this, MainActivity.class);
// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
// PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
// String channelId = createNotificationChannel("my_channel_ID", "my_channel_NAME", NotificationManager.IMPORTANCE_HIGH);
// // 构建 remoteView
//// RemoteViews remoteView = new RemoteViews(getPackageName(), R.layout.notification_message);
//
//// remoteView.setTextViewText(R.id.tvMsg, "RemoteViews");
//// remoteView.setImageViewResource(R.id.ivIcon, R.mipmap.ic_launcher_round);
// NotificationCompat.Builder builder = new NotificationCompat.Builder(this,channelId);
// // 设置自定义 RemoteViews
// builder.setContent(remoteView).setSmallIcon(R.mipmap.ic_launcher);
// // 设置通知的优先级(悬浮通知)
// builder.setPriority(NotificationCompat.PRIORITY_MAX);
// builder.setContentIntent(pendingIntent);
// builder.setAutoCancel(true);
// Notification notification = builder.build();
// NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
// manager.notify(0, notification);
//
// }
}
*/